Device Lab Exercises
Exercise 5 (Intermediate)
Geolocation
Goal: Use geolocation to define the latitude and longitude of the
device’s current location.
The most basic function of geolocation is to retrieve the latitude and
longitude positions of the user, so that you can locate the user on a
map. The sensor could also be used to retrieve the following additional
properties:
altitude -The altitude in meters.
heading - The direction of movement (true north) in integer degrees.
latitude - The latitude in degrees.
longitude - The longitude in degrees.
horizontalAccuracy - The horizontal accuracy in meters.
verticalAccuracy - The vertical accuracy in meters.
speed - The speed in meters/second.
timestamp - The number of milliseconds at the time of the event since
the runtime was initialized.
Exercise
1.
Select the first frame in the Actions layer and open the Actions
panel.
2. Import the geolocation and GeolocationEvent classes:
import flash.events.GeolocationEvent;
import flash.sensors.Geolocation;
3.
Create a conditional statement that checks to see if geolocation is
supported. If it isn’t supported, display “No geolocation support”
in the textBox. (Note that geolocation isn’t supported in the Flash
Player in the browser.)
if (Geolocation.isSupported)
{
}
else
{
feedback_txt.text = “No geolocation support.”;
}
Within the if statement, create a new geolocation variable and
define how often (in millisectionds) the event updates:
if (Geolocation.isSupported)
{
var geo:Geolocation = new Geolocation();
geo = new Geolocation();
geo.setRequestedUpdateInterval(100);
}
4.
5. Test the file in Device Central.
6. Expect the following error:
VerifyError: Error #1014: Class flash.
sensors::Geolocation could not be found.
Copyright 2010 Paul Trani
Within the if statement, add an update listener that calls function
named geolocationUpdateHandler whenever there’s an update
to the geolocation instance:
if (Geolocation.isSupported)
{
var geo:Geolocation = new Geolocation();
geo = new Geolocation();
geo.setRequestedUpdateInterval(100);
geo.addEventListener(GeolocationEvent.UPDATE,
geolocationUpdateHandler);
}
7.
Displaying Latitude, Longitude and Horizontal Accuracy
Create the geolocationUpdateHandler function and display the
latitude of the devices in the lat_txt text field.
function geolocationUpdateHandler(event:GeolocationEvent):void
{
lat_txt.text = “Latitude: “ + event.latitude;
}
9. Display the longitude of the devices in the long_txt text field
and the accuracy (in meters)
function geolocationUpdateHandler(event:GeolocationEvent
):void
{
lat_txt.text = “Latitude: “ + event.latitude;
long_txt.text = “Longitude: “ + event.longitude;
accuracy_txt.text = “Horizontal Accuracy: “ +
event.horizontalAccuracy;
}
10. Final code:
8.
import flash.events.GeolocationEvent;
import flash.sensors.Geolocation;
if (Geolocation.isSupported)
{
var geo:Geolocation = new Geolocation();
geo.setRequestedUpdateInterval( 100 );
geo.addEventListener( GeolocationEvent.UPDATE, doGeoUpdate);
}
else
{
feedback_txt.text = “Geolocation not supported”;
}
function doGeoUpdate( event:GeolocationEvent ):void
{
Device Lab Exercises
lat_txt.text = “Latitude: “ + event.latitude;
long_txt.text = “Longitude: “ + event.longitude;
accuracy_txt.text = “Horizontal Accuracy: “ + event.
horizontalAccuracy;
}
9. Select the Permissions tab.
10. Select the “ACESS_FINE_LOCATION” to allow permission to use the
device’s geolocation capability.
11. The file must be published as an AIR app in order to see the results.
Publishing the File
(Refer to Exercise 1 for setup and more detailed publishing instructions)
1. With the Flash file open, go to the Publish Settings: File > Publish
Settings...
2. Select the Flash tab.
3. Change the Flash version to AIR Android.
4.
5.
•
•
•
Select Settings...
Add the following information:
Output file: Ex5_(yourname).apk (e.g. Ex2_PaulTrani.apk)
App name: Ex5_(yourname)
App ID (usually a reverse domain): com.(yourname).ex5
6.
7.
8.
Select the Deployment tab.
Load or create a Certificate.
Select “Install application on the connected Android device”.
Copyright 2010 Paul Trani
11.
12.
13.
14.
Select “Launch application on the connected Android device”.
Define the location of the adb file that is part of the Android SDK.
Select Publish to publish the app to the connected device.
Congratulate yourself on a job well done.
© Copyright 2026 Paperzz