miércoles, 14 de septiembre de 2011

How to get GPS data from Playbook and save in a File and read it.

import flash.sensors.Geolocation;
import flashx.textLayout.factory.StringTextLineFactory;
import mx.events.FlexEvent;
import qnx.media.QNXStageWebView;
private var geoLocation:Geolocation;
private var ObjfiletoSave:File;
private var Objstream:FileStream = new FileStream();
private var Objstream2:FileStream = new FileStream();
private var Count:Number =0;
private var Coord: String="";

protected function application1_applicationCompleteHandler(event:FlexEvent):void {
lectura.text="file applicationdirectory: "+File.applicationDirectory.nativePath +"\n"
+"file applicationStorageDirectory: "+File.applicationStorageDirectory.nativePath +"\n"
+"file desktopDirectory: "+File.desktopDirectory.nativePath +"\n"
+"file documentsDirectory: "+File.documentsDirectory.nativePath +"\n"
+"file userDirectory: "+File.userDirectory.nativePath +"\n"
// TODO Auto-generated method stub
if(Geolocation.isSupported==true){
geoLocation = new Geolocation();
geoLocation.setRequestedUpdateInterval(1000);
geoLocation.addEventListener(GeolocationEvent.UPDATE,handleLocateRequest);
}else{
status.text="No soporta Geolocalización!"
}
}
private function handleLocateRequest(event:GeolocationEvent):void{
var mph:Number = event.speed * 2.23693629;
var kph:Number = event.speed * 3.6;
info.text="Actualización: " + new Date().toTimeString() + "\n\n"
+ "latitud: " + event.latitude.toString() + "\n"
+ "longitud: " + event.longitude.toString() + "\n"
+ "altitud: " + event.altitude.toString() + "\n"
+ "speed: " + event.speed.toString() + "\n"
+ "speed: " + mph.toString() + "\n"
+ "speed: " + kph.toString() + "\n"
+ "cabecera: " + event.heading.toString()+ "\n"
+ "horizontal accuracy: " + event.horizontalAccuracy.toString() + "\n"
+ "vertical accuracy: " + event.verticalAccuracy.toString() ;
SaveinTextFile(event.latitude.toString(),event.longitude.toString());
}

private function SaveinTextFile(Latitud:String,Longitud:String):void{
var Fecha:String = new Date().toTimeString();
ObjfiletoSave=File.documentsDirectory.resolvePath("samples/GPS.txt");
status.text="Salva Lectura de " + File.documentsDirectory.nativePath + "/samples/GPS.txt";
Objstream.open(ObjfiletoSave, FileMode.APPEND);
Objstream.writeUTFBytes("var Count"+ Count.toString() +"=new GLatLng("+ Latitud + "," + Longitud + "); \n" )
Coord= Coord + "Count" + Count.toString()+",";
Count ++;
}

private function ReadTextFile():void{
ObjfiletoSave=File.documentsDirectory.resolvePath("samples/GPS.txt");
status.text="Lectura de " + File.documentsDirectory.name + "/samples/GPS.txt";
var Objstream2:FileStream = new FileStream();
Objstream.open(ObjfiletoSave, FileMode.READ);
lectura.text = Objstream.readUTFBytes(Objstream.bytesAvailable);
Objstream.close();
//status.text="Lectura..." }
protected function detener_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
geoLocation.removeEventListener(GeolocationEvent.UPDATE,handleLocateRequest);
status.text="Lectura detenida";
Objstream.writeUTFBytes(Coord );
Objstream.close();
}

protected function mostrar_clickHandler(event:MouseEvent):void {
// TODO Auto-generated method stub
ReadTextFile();
}

protected function continua_clickHandler(event:MouseEvent):void {
// TODO Auto-generated method stub
geoLocation = new Geolocation();
geoLocation.setRequestedUpdateInterval(1000);
geoLocation.addEventListener(GeolocationEvent.UPDATE,handleLocateRequest);
}