Geolocation. Best practices
Hi at all,
i wrote this "GeolocationService" class, i would like to have your opinion.
Thanks!
F.
package services
{
import flash.events.Event;
import flash.events.GeolocationEvent;
import flash.events.IEventDispatcher;
import flash.events.StatusEvent;
import flash.sensors.Geolocation;
public class GeoService
{
public var dispatcher:IEventDispatcher;
public var geolocation:Geolocation;
public var latitude:Number;
public var longitude:Number;
public var gpsSupported:Boolean;
public var gpsEnabled:Boolean;
public static const ENABLE_GPS:String = "enableGPS";
public static const DISABLE_GPS:String = "disableGPS";
public function GeoService()
{
if(Geolocation.isSupported) {
gpsSupported = true;
geolocation = new Geolocation();
trace("Geolocation created");
geolocation.setRequestedUpdateInterval(10000);
if(geolocation.muted) {
// inform the user to turn on the location sensor
} else {
activeGPS();
}
geolocation.addEventListener(StatusEvent.STATUS, onStatusChange);
} else {
gpsSupported = false;
}
}
protected function onStatusChange(event:StatusEvent):void
{
if(geolocation.muted) {
// inform the user to turn on the location sensor
deactiveGPS();
} else {
activeGPS();
}
}
protected function onUpdate(event:GeolocationEvent):void
{
latitude = event.latitude;
longitude = event.longitude;
}
public function activeGPS():void
{
if(gpsSupported && geolocation && !geolocation.hasEventListener(GeolocationEvent.UPDATE))
{
geolocation.addEventListener(GeolocationEvent.UPDATE, onUpdate);
gpsEnabled = true;
}
}
public function deactiveGPS():void
{
if(gpsSupported && geolocation && geolocation.hasEventListener(GeolocationEvent.UPDATE))
{
geolocation.removeEventListener(GeolocationEvent.UPDATE, onUpdate);
gpsEnabled = false;
}
}
}
}
