Skip to main content
Participating Frequently
August 8, 2011
Question

Geolocation. Best practices

  • August 8, 2011
  • 1 reply
  • 2205 views

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;
            }           
        }   
    }
}

This topic has been closed for replies.

1 reply

Inspiring
August 9, 2011

May be you should look at the precision of the value received. I would increase the number of queries to the GPS until the precision is acceptable and then I would set it to 10 sec or 20 sec.

Participating Frequently
August 10, 2011

Hi Rangora

thanks for your reply.

I'm sorry but i I did not understand when you said : I would increase the number of queries to the GPS until the precision  is acceptable and then I would set it to 10 sec or 20 sec.

Latitude and longitude should be sent to the server to store the location of the device.

Could you explain with an example?

Thanks! F.

Inspiring
August 10, 2011

var attempts:int = 0;

var HAccuracy:Number = 100000;

if (Geolocation.isSupported)
{
       gps = new Geolocation();
       if (!gps.muted)
       {
               gps.addEventListener(GeolocationEvent.UPDATE, gpsUpdate);
               gps.setRequestedUpdateInterval(3000);
       }
}

private function gpsUpdate(event:GeolocationEvent):void
{

     if (event.horizontalAccuracy < 100)
     {
            HAccuracy = event.horizontalAccuracy;
            gps.setRequestedUpdateInterval(15000);
     }
     else
     {
           if ((HAccuracy - event.horizontalAccuracy < 10))
           {
                  if (attempts++ > 10)
                         gps.setRequestedUpdateInterval(15000);       
           }

           else

           {

               attempts = 0;

               HAccuracy = event.horizontalAccuracy;

           }

     }

}

It might not be needed if you doesn't display the position to the user and just send it to a server since you doesnt need to display an accurate position as soon as you can.