Thursday, 6 September 2012

Geolocation in AIR Android

Geolocation is a feature in AIR Android that provides the GPS mobile location using the Geolocation and GeolocationEvent classes. To use this feature you need to check the ‘ACCESS_FINE_LOCATION’ check box in the Permissions tab in the Android settings, or you can also enable the geolocation inside the application descriptor file.

<android>
    <manifestAdditions>
      <![CDATA[<manifest>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>]]>
    </manifestAdditions>
  </android>

Here is an overview of the Geolocation class. Firstly, the isSupported property checks if the geolocation service is supported. The setRequestedUpdateInterval() method sets time intervals for location updates. The Geolocation.UPDATE event returns the following: altitude, heading, latitude, longitude, speed, timestamp, horizontalAccuracy and verticalAccuracy. Finally, the StatusEvent.STATUS event is used to determine if there is access to the geolocation sensor.

if(Geolocation.isSupported)
{
 var geolocation:Geolocation = new Geolocation();
 geolocation.setRequestedUpdateInterval();
 geolocation.addEventListener(Geolocation.UPDATE, updateHandler);
 geolocation.addEventListener(StatusEvent.STATUS, statusHandler);
}
This is a simple example that displays the data from the Geolocation.UPDATE event into a text field on the stage.
//Sets up the stage
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

//Add text field on the stage.
var tf:TextField = new TextField();
tf.width = stage.stageWidth;
addChild(tf);

if(Geolocation.isSupported)
{
 var geolocation:Geolocation = new Geolocation();
 geolocation.setRequestedUpdateInterval(5000);
 geolocation.addEventListener(GeolocationEvent.UPDATE, updateHandler);
 geolocation.addEventListener(StatusEvent.STATUS, statusHandler);
}

function updateHandler(e:GeolocationEvent):void
{
 tf.text = 'Location data' + "\n";
 tf.appendText('Altitude: ' + e.altitude + "\n");
 tf.appendText('Heading: ' + e.heading + "\n");
 tf.appendText('Latitude: ' + e.latitude + "\n");
 tf.appendText('Longitude: '+ e.longitude + "\n");
 tf.appendText('HorizontalAccuracy: '+ e.horizontalAccuracy + "\n");
 tf.appendText('VerticalAccuracy: '+ e.verticalAccuracy);
}

function statusHandler(e:StatusEvent):void
{
 tf.text = 'Geolocation is not available';
}

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP