Using GeolocationThe following code sample demonstrates the functioning of geolocation. The code first checks if the device supports geolocation. If it does, then an event listener is registered to listen for changes in the location after the application has started. Whenever the listener detects a change, the gpsListener function is called and it returns the different coordinate information. The time interval for acceleration updates is set at 5 seconds. For more information, see Geolocation. import flash.sensors.*;
if (Geolocation.isSupported)
{
var gps:Geolocation = new Geolocation();
function gpsListener(evt:GeolocationEvent):void
{
trace("Latitude: " + evt.latitude.toString());
trace("Longitude: " + evt.longitude.toString());
trace("Altitude: " + evt.altitude.toString());
trace("Horizontal Accuracy: " + evt.horizontalAccuracy.toString());
trace("Vertical Accuracy: " + evt.verticalAccuracy.toString());
trace("Speed: " + evt.speed.toString());
trace("Heading: " + evt.heading.toString());
trace("Timestamp: " + evt.timestamp.toString());
}
gps.addEventListener(GeolocationEvent.UPDATE, gpsListener);
gps.setRequestedUpdateInterval(5000);
gps.removeEventListener(GeolocationEvent.UPDATE, gpsListener);
}
else
{
trace("GPS not supported.");
}
For details about the Geolocation and GeolocationEvent classes, see Adobe ActionScript 3.0 Reference for the Adobe Flash Platform. |
|