Flex Pasta » Flex 4.5: Using Geolocation(GPS) on a mobile device

Flex 4.5: Using Geolocation(GPS) on a mobile device

When using the Flex 4.5 SDK for mobile application, there is a built in Geolocation class for reading information about a user’s current GPS location.  Here is a simple example of how it works:

private var geo:Geolocation = new Geolocation();

private function gpsRun():void

{

geo.addEventListener(GeolocationEvent.UPDATE, updateLocation);

}

private function updateLocation(geoEvent:GeolocationEvent):void

{

geo.removeEventListener(GeolocationEvent.UPDATE, updateLocation);

}

 

s:creationComplete

Geolocation.isSupported =  Is this device a GPS device?

geo.muted = The device is a GPS device but the GPS has been disabled.

In this example, when the Geolocation is supported and not muted, the GeolocationEvent.UPDATE event listener is added.  Assume that the target device for this application is a mobile device which will have geolocation and it will also not be muted.  When the event listener GeolocationEvent.UPDATE is added, the device will immediately fire its gps signal and begin searching for a location.  A phone’s GPS is one of the biggest drainers of the phone’s battery.  To shut off the GPS, the event listener must be removed(as shown above).  Without removing the event listener, the GPS on the device will run until the application is killed.  With that in mind, there is another issue that comes in to play with the above example.  To get an accurate reading, the application will need to listen for GPS for indeterminate amounts of time.  Just listening for one GeolocationEvent.UPDATE will likely result in the accuracy being off be hundreds of miles!

How much event listening time is needed to get an accurate GPS location?

One update event is not enough to get even a close GPS reading.  However, listening forever is also not desired as battery drainage will occur.  Further complicating the issue is some users might be indoors, others might have better hardware, and others may be in more prime GPS locations.  There is really no way to tell how long it will take to get an accurate reading.  Putting a timer or a count on the number of times the event listener fires will result in mixed results.  And of course it all depends how important an accurate and up to date GPS location is to the application.

There are a set of properties on the GeolocationEvent.  Besides latitude and longitude, there properties for horizontalAccuracy(horizontal accuracy in meters), verticalAccuracy(vertical accuracy in meters), altitude(altitude in meters), speed(meters per second).  After quite a bit of GPS testing with a Droid, moving indoors and outdoors, it seems a good location event happens when the horizontalAccuracy is lower than 100 meters.  It is worth testing at different locations and on a variety of devices.  Removing the event listener and turning off the GPS seems to work when the horizontal accuracy falls below 100.

What do the Android Docs say about GPS accuracy?

According to the Google Android docs, GPS accuracy is broken down into three categories:

High Accuracy - Less than 100 meters in horizontal accuracy

Medium Accuracy - Between 100-500 meters in horizontal accuracy

Low Accuracy - Greater than 500 meters in horizontal accuracy

The high accuracy of 100 meters or less is close to what AIR for Android reported for an accurate location.  Following the guide for Google, it is fair to say that the above accuracy levels can be used when determining when to keep listening for the GeolocationEvent and when to stop it.  This will help get the best possible GPS location without draining the battery.

4 Comments

  • 1. Juan Mendez replies at 31st May 2011, 9:22 am :

    It is also useful to know that whenever the application hides, we can also remove our listener to geolocation updates. In the same way if the application goes back to the screen.

    NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, deactiveHandler);

  • 2. Vivek replies at 13th September 2011, 4:36 am :

    How can we use it in MXML file

  • 3. Jonathan replies at 18th September 2011, 7:30 pm :

    I have a g-tablet with connected Bluetooth GPS working in other programs but it won’t fire an event in Flex. Any ideas? Does flex support mock locations? Are there other GPS librarys to try?

  • 4. Putting the “Commun&hellip replies at 30th September 2011, 4:17 pm :

    […] Brian Telintelo Using Geolocation on a mobile device […]

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>