This article shows how to display current phone GPS position using Flash Lite, KuneriLite, and Google Static Maps.
Contents |
NOTE: Usage of this code with the free Google Maps API Key breaks Google's Terms and Conditions (section 10.8). You should purchase an Enterprise License if you wish to use the Google Maps API as shown in this example.
To use Google Maps services, you need a Google Maps API key. If you do not have one, you can sign up for a key here: http://code.google.com/apis/maps/signup.html
KuneriLite is a toolkit that extends Flash Lite capabilites, allowing applications to access native Symbian OS functionalities, such as file writing or reading GPS data.
To proceed in this tutorial, download and install KuneriLite from the
KuneriLite download page.
In this example, Flash Lite 2.1 is used. However, porting it to other (older or newer) Flash Lite versions is quite straightforward. So, after you have created an empty Flash Lite movie, do as follows:
In the movie root, create a new layer called Actions, and open its ActionScript editor. First define the properties:
// Enter your api key here
var apiKey = 'API_KEY';
//If you use the non-commercial version of KuneriLite, you do not need to change this
var kuneriPath = 'http://127.0.0.1:1001/Basic/';
Next, define some useful functions to be used in the code:
//This function is called when some KuneriLite-related errors occur
function kuneriError(error:String)
{
trace("KuneriLite error: " + error);
}
//This function will do all calls to KuneriLite servers
//and call the given handler passing response values as the argument
function kuneriLoad(url, handler)
{
var loader:LoadVars = new LoadVars();
loader.onLoad = function()
{
handler(this);
}
trace("LOADING: " + url);
loader.load(url);
}
Next, code the button-related logic. When the user presses startButton, the application should:
To get full information about the KuneriLite GPS plug-in, check the related Wiki page:http://wiki.kunerilite.net/index.php?title=GPS_plugin The GPS is started when the gpsButton is pressed, using the start klCommand:
startButton.onPress = function()
{
kuneriLoad(kuneriPath + 'GPS?klCommand=start', gpsStarted);
}
function gpsStarted(res:LoadVars)
{
if(res.klError == 0 || res.klError == -11)
{
trace("GPS started");
kuneriLoad(kuneriPath + 'GPS?klCommand=read', gpsDataRead);
}
else
{
kuneriError("Error starting GPS!");
}
}
The gpsStarted() handler will:
http://wiki.kunerilite.net/index.php?title=GPS_plugin
The second call to KuneriLite will call the gpsDataRead() handler defined below:
function gpsDataRead(res:LoadVars)
{
if(res.klError == 0)
{
var lat = res.klPosLatitude;
var lng = res.klPosLongitude;
trace("POSITION: " + lat + ", " + lng);
loadMap(lat, lng);
}
else
{
kuneriError("Error starting GPS!");
}
}
This handler, as above, checks if there are any errors raised by KuneriLite and if not, extracts latitute and longidute coordinates from the response's klPosLatitude and klPosLongitude properties. After this, it calls the loadMap() function that actually loads the static map image.
function loadMap(lat:Number, lng:Number)
{
var mapClip:MovieClip = _root.createEmptyMovieClip('mapClip', _root.getNextHighestDepth());
mapClip._x = 0;
mapClip._y = 0;
var mapWidth = 240;
var mapHeight = 280;
var loader:MovieClipLoader = new MovieClipLoader();
var mapUrl:String = 'http://maps.google.com/staticmap?center=' +
lat + ',' + lng + '&format=jpg&zoom=8&size=' +
mapWidth + 'x' + mapHeight + '&key=' + apiKey;
loader.loadClip(mapUrl, mapClip);
}
The above function:
After this, you can actually test your Flash Lite movie.
To test your application without deploying it on a real device:
For more information about the KuneriLite emulator, see KuneriLite Emulator
To test your application on a real device, package your SIS application using the KuneriLite Wizard:
For more information about KuneriLite Wizard, see the KuneriLite Wizard Beginner's Guide.