Using the Geolocation Sensor in Flash Using AS3

By Blue_Chi | Flash CS5 | ActionScript 3.0 | Beginner

This tutorial will teach you how to use the Geolocation Sensor in a Flash movie your mobile device to retrieve the geographical position of the user. This feature is only compatible with Flash Lite 4, Adobe AIR Mobile, and the iPhone.

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 additional details such as the speed at which the user is moving.

This is a beginner AS3 tutorial that will only require you to know the basics of AS3 Variables and Event Handling.

Starting Off

This tutorial can be followed as a Flash Lite 4 project, an Adobe AIR project, and an iPhone project. We are going to create a Flash Lite 4 project because it is easier to test the device in Device Central using this format as AIR Mobile and iPhone applications cannot be tested in Device Central at the time of writing this tutorial.

Our tutorial will simply retrieve the geolocation data and display them on the screen inside a text field.

Start up Flash CS5 and go through New>Flash Lite 4 to create a new Flash Lite 4 movie. We do not need any graphical elements on the screen as we can create our text field by using AS3 later. Just right-click the only frame you have on the timeline and select Actions to open up the Actions Panel.

We are going to begin our code, the first step is importing the classes needed to use the geolocation sensor - the Geolocation Class and the GeolocationEvent Class:

import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;

We can now use geolocation capabilities in our project, but before we do that we need to detect whether the device that will run the movie supports geolocation - as you should not not all mobile phones have GPS features. It is necessary to make sure that you can have an alternative option or a warning message if the user's device doesn't support geolocation or if the user has disabled geolocation access to your application. We can check for geolocation support by using the isSupported() method of the Geolocation Class:

import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;

if (Geolocation.isSupported){



}
You can learn more about the AS3 if Conditional by reviewing our tutorial on this topic.


The method above will return the value true if geolocation is supported and therefore whatever we put between the curly brackets of the conditional will only then be executed.

Our first action to carry out once we have verified that geolocation is support is to create an instance of our geolocation class. In order to use any ActionScript Class we have to create an instance of it and then play around with that instance of the class. To create an instance of the class we use the new keyword. We are going to call our instance my_geo:

import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;

if (Geolocation.isSupported){

var my_geo:Geolocation = new Geolocation();


}
You can learn more about AS3 Variables and Classes by reviewing our tutorial on this topic.

The geolocation class sends updates on the status of the location repeatedly, we have to register the geolocation update event and retrieve the data we need from an event listener function that we will have that as its specific task.

import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;

if (Geolocation.isSupported){

var my_geo:Geolocation = new Geolocation();
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);

}
You can learn more about AS3 Event Handling by reviewing our tutorial on this topic.

We now need to create a text field which will display our location coordinates. We can do that easily by using the TextField Class, once created we will make the textfield wrap the text properly in case the longitude values are long and then display the empty text field on the screen by using the Display List method DisplayChild():

import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;

if (Geolocation.isSupported){

var my_geo:Geolocation = new Geolocation();
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);

var my_txt:TextField = new TextField();
my_txt.wordWrap=true;
addChild(my_txt);

}
You can learn more about AS3 TextField Class and AS3 Display List by reviewing our tutorials on these topics.

It is now time to create the function that will retrieve our geolocation details. We can define this function outside the conditional as it will only be triggered if called by the event listener. The function will set the value of the text field by using the latitude and longitude properties of the GeolocationEvent instance:

import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;

if (Geolocation.isSupported){

var my_geo:Geolocation = new Geolocation();
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);

var my_txt:TextField = new TextField();
my_txt.wordWrap=true;
addChild(my_txt);

}

function onGeoUpdate(e:GeolocationEvent):void{
my_txt.text = "My Latitude is "+e.latitude+" and my Longitude is "+e.longitude;
}

We are set, this is everything you need to use the Geolocation Sensor on your mobile device, you can test your movie by going through Control>Test>In Device Central. This should launch Device Central and play your movie, you might have to set the Test Device in Device Central so that you are using a Flash Lite 4 device:

AS3 Geolocation Sensor

Now you access the Geolocation Panel on the right-side of the window (or by going through Window>Flash>Geolocation if you can't see it). Inside this panel you can manually put values for the latitude and longitude which you want your mobile to emulate. You can put any value here and then click on Send to Device. Once you do that you should see your values appear inside the emulator.

AS3 Geolocation

Our use is obviously basic, but it shows you how to retrieve the location data.

Update Frequency

In a real world application your location would change as you move around, the frequency at which Flash will be updated about changes in your location can be configured by using the setRequestedUpdateInterval() method. If you do not use this method the updates will depend on the default values of your device which vary from one device to another. This method can be used directly on an instance of the Geolocation Class and accepts a value in milliseconds this way:

import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;

if (Geolocation.isSupported){

var my_geo:Geolocation = new Geolocation();
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
my_geo.setRequestedUpdateInterval(50);

var my_txt:TextField = new TextField();
my_txt.wordWrap=true;
addChild(my_txt);

}

function onGeoUpdate(e:GeolocationEvent):void{
my_txt.text = "My Latitude is "+e.latitude+" and my Longitude is "+e.longitude;
}

You should be careful about setting very short intervals for your geolocation sensor as that will rapidly consume the battery of your device. You should always unregister your event after you are done retrieving the data that you need as well.

Additional Values

In addition to the latitude and longitude values which we have used in our example above you can also retrieve the following pieces of information:

  • altitude—The altitude value.
  • heading—The angle in respect to north in which the user is heading.
  • speed—The speed in meters per second.
  • verticalAccuracy—The vertical accuracy.
  • horizontalAccuracy—The horizontal accuracy.

These values would only be helpful to someone creating an advanced location based application such as a navigation system.

This concludes our tutorial, you can try integrating geolocation with a Google Maps application to show the location on a map. You should note however that the Google Maps application does not work in iPhone Flash Applications as it requires loading code at runtime. If you have any questions feel free to post them at the Republic of Code Forum.

- End of Tutorial