10 October 2011
Intermediate
The Geolocation API provides a high-level interface to access the geographical location information of the device. The geographical location can be displayed on the device in the form of latitudinal and longitudinal coordinates. The API is designed to enable both one-shot location request and repeated location updates (useful while building a Geo tracking application). This example shows you how you can get repeated location updates in your application.
In your Android device, when the location of the device changes, your application can receive updates about the changes, including information on altitude, accuracy, heading, speed, and timestamp using one of the following location sources:
The Geolocation API provides the necessary support to build applications, which uses the location sources configured in the device.
In this article, you will learn about:
To build Geolocation-based applications, use the following classes:
flash.sensors.Geolocation to initilialize location sensor in your device.flash.events.GeolocationEvent to get location information at defined intervals.To find out if the device supports Geolocation at all, use the Geolocation.isSupported property. This property is false on devices that do not have geolocation capabilities. This property will also return false in an Android emulator, even when you simulate the Geolocation functionality with the Android Emulator's geo fix command, and hence this example will not work in an Android emulator.
The following code initializes your device's location sensor and adds an event listener to get regular location updates from the device:
if (Geolocation.isSupported)
{
//Initialize the location sensor.
geo = new Geolocation();
if(! geo.muted){
geo.setRequestedUpdateInterval(60000);
//Register to receive location updates.
geo.addEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
}
}
In the previous example, the time interval for getting location updates is set to 60000 milliseconds (1 minute). You should choose the longest update interval that satisfies your application requirements.
Note: If you do not set the setRequestUpdateInterval property, the device will return the location updates based on the default interval set in the device. To conserve battery power, it is always advisable to provide a longer update interval.
The application can stop performing other activities if the location sources are not available by reading the muted property of the Geolocation class:
if(geo.muted){
//Display an error message or halt the application.
}
In the geolocationUpdateHandler function, use the GeolocationEvent's properties to get the location information:
private function geolocationUpdateHandler(event:GeolocationEvent):void
{
geoLat.text = "Latitude: " + event.latitude.toString();
geoLong.text = "Longitude: " + event.longitude.toString();
geoAccu.text = "Accuracy: " + event.horizontalAccuracy.toString();
geoSpeed.text = "Speed: " + event.speed.toString();
}

Table 1 shows the Geolocation attributes reported by the GeolocationEvent object.
Table 1. Geolocation properties of the GeolocationEvent object
| Attribute | Description | Unit |
|---|---|---|
latitude |
The geographical coordinate with a value between –90 to +90 degrees. Negative latitude denotes south and positive latitude denotes north. | Degrees |
longitude |
The geographical coordinate with a value between –180 to +180 degrees. Negative longitude denotes west and positive longitude denotes east. | Degrees |
altitude |
Vertical height of the geographical coordinates (Latitude and Longitude). | Meters |
horizontalAccuracy (Accuracy) |
The horizontal accuracy of the position denoted by the geographical coordinates (Latitude and Longitude). | Meters |
verticalAccuracy (Altitude Accuracy) |
The vertical accuracy of the position denoted by the geographical coordinates (Latitude and Longitude). | Meters |
speed |
The current ground speed of the device. If the device is stationary, the value of this attribute will be zero. | Meters/Seconds |
timestamp |
The time since the application was initialized. For example, if the application has captured geolocation data for four seconds after it got initialized, then the value of this attribute will be 4000. | Milliseconds |
Note: Although the Heading Geolocation attribute is available through the API, it is not yet supported in the Android devices. The Heading attribute will always return NaN (Not a Number) in an Android application.
For your application to work, you need to specify the necessary Android permission in the application descriptor file under the <android> element.
<android>
<manifestAdditions>
<![CDATA[<manifest>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>]]>
</manifestAdditions>
</android>
Note that the ACCESS_FINE_LOCATION Android permission is required by the application to use the device's GPS sensor.
When your application does not return any value for the Geolocation attributes, it could be because of one or more of the following issues:

Figure 2. Checking the Internet APN configuration.

Figure 3. Checking whether GPS is enabled.
You can enable both GPS-based and network-based location sources so that if the GPS fix does not happen, your AIR application will try to use the network-based location source (if data connection is available).
ACCESS_FINE_LOCATION Android permission as discussed in the "Declaring Android permission" section above. The ACCESS_FINE_LOCATION permission is required to access GPS location data. Either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION is required to access wireless network location data. public function locateMe()
{
Geolocation geo = new Geolocation();
...
}
private var geo:Geolocation;
public function locateMe()
{
geo = new Geolocation();
...
}
If you still face any issue getting the Geolocation API to work in your application, post your issue in the AIR for Android developer forum.
In this example, you have learned how to use the AIR Geolocation APIs for your Android applications. You can use these Geolocation APIs to build simple-to-complex Android applications involving navigation, speed monitoring, and GPS-based route tracking.
For more information, refer to the following resources:

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License