GPS stands for Global Positioning System. GPS is a space-based satellite navigation system, which provides reliable location information to your handheld device.
If your application requires the use of the device’s GPS, you will need to select the ACCESS_FINE_LOCATION permission when you are creating your project. See Chapter 3 for help with permissions.
Let’s review the code below. First, you will notice that there is a
private variable named geoLocation
declared, of type
flash.sensors.GeoLocation
. Within
application
Complete
of the application, an event
handler function is called, which first checks to see if the device has an available GPS
unit by reading the static property of the GeoLocation
class. If this property returns
as true, a new instance of GeoLocation
is created; the data refresh interval is set to
500 milliseconds (.5 seconds) within the setRequestedUpdateInterval
method;
and an event listener of type GeoLocation
Event.UPDATE
is added to handle updates.
Upon update, the GPS information is read from the event and written to a
TextArea
within the handleUpdate
function.
Note
There is also some math being done to convert the speed property into miles per hour and kilometers per hour.
The results can be seen within Figure 4-2:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" applicationComplete="application1_applicationCompleteHandler(event)"> <fx:Script> <![CDATA[ import mx.events.FlexEvent; import flash.sensors.Geolocation; privatevar geoLocation:Geolocation; protectedfunction application1_applicationCompleteHandler (event:FlexEvent):void { if(Geolocation.isSupported==true){ geoLocation = new Geolocation(); geoLocation.setRequestedUpdateInterval(500); geoLocation.addEventListener(GeolocationEvent.UPDATE, handleLocationRequest); } else { status.text = "Geolocation feature not supported"; } } privatefunction handleLocationRequest(event:GeolocationEvent):void { var mph:Number = event.speed*2.23693629; var kph:Number = event.speed*3.6; info.text = "Updated: " + new Date().toTimeString() + "\n\n" + "latitude: " + event.latitude.toString() + "\n" + "longitude: " + event.longitude.toString() + "\n" + "altitude: " + event.altitude.toString() + "\n" + "speed: " + event.speed.toString() + "\n" + "speed: " + mph.toString() + " MPH \n" + "speed: " + kph.toString() + " KPH \n" + "heading: " + event.heading.toString() + "\n" + "horizontal accuracy: " + event.horizontalAccuracy.toString() + "\n" + "vertical accuracy: " + event.verticalAccuracy.toString(); } ]]> </fx:Script> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <s:Label id="status" text="Geolocation Info" top="10" width="100%" textAlign="center"/> <s:TextArea id="info" width="100%" top="40" editable="false"/> </s:Application>
Get Developing Android Applications with Flex 4.5 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.