Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [55]

By Root 2471 0

import flash.net.navigateToURL;

import flash.net.URLRequest;

import flash.sensors.Geolocation;

function onTravel(event:GeolocationEvent):void {

geolocation.removeEventListener(GeolocationEvent.UPDATE, onTravel);

var long:String = event.longitude.toString();

var lat:String = event.latitude.toString();

navigateToURL(

new URLRequest("http://maps.google.com/?q=" + lat + "," + long));

}

Note that if you navigate to http://maps.yahoo.com instead, launching the native Google Maps is not an option.

The major hurdle with this approach is that your application is now in the background and there is no direct way to go back to it unless you press the device’s native back button.

The Android SDK has a library for embedding maps into native applications with interactivity. AIR doesn’t support this feature at the time of this writing, but there are many other ways to offer a map experience to your audience. To demonstrate some of the map features within AIR, we will use the Yahoo! Maps Web Services (http://developer.yahoo.com/maps) and Google Maps API family (http://code.google.com/apis/maps/).

Static Maps


A static map may be sufficient for your needs. It provides a snapshot of a location; although it doesn’t offer pan or zoom, it does load relatively quickly, even over GPS.

The Yahoo! Map Image API


The Yahoo! Map Image API from Yahoo! Maps (http://developer.yahoo.com/maps/rest/V1/) provides a reference to a static map image based on user-specified parameters. This API requires an applicationID. It doesn’t set a restriction on how to use the service, it serves images up to 1,024×1,024, and it has few customizable options.

To use the API, send a URLRequest with your parameters. In return, you receive the path to the image which you then load using a Loader object. The next example uses the point location from geolocation, the stage dimensions for the image size, and 1 for street level (zoom goes up to 12 for country level):

import flash.display.Loader;

import flash.events.GeolocationEvent;

import flash.events.Event;

import flash.net.URLLoader;

import flash.net.URLRequest;

import flash.sensors.Geolocation;

var geolocation:Geolocation;

const YAHOO_URL:String =

"http://local.yahooapis.com/MapsService/V1/mapImage";

const applicationID:String = "YOUR_YAHOO_APP_ID";

var urlLoader:URLLoader;

var loader:Loader;

function findLocation():void {

if (Geolocation.isSupported) {

geolocation = new Geolocation();

geolocation.addEventListener(GeolocationEvent.UPDATE, onTravel);

}

}

function onTravel(event:GeolocationEvent):void {

var request:String = "?appid=YOUR_APPI"

+ "&latitude=" + event.latitude

+ "&longitude=" + event.longitude

+ "&zoom=1"

+ "&image_height=" + stage.stageHeight

+ "&image_width=" + stage.stageWidth;

urlLoader = new URLLoader();

urlLoader.addEventListener(Event.COMPLETE, onXMLReceived);

urlLoader.load(new URLRequest(YAHOO_URL + request));

}

function onXMLReceived(event:Event):void {

urlLoader.removeEventListener(Event.COMPLETE, onXMLReceived);

geolocation.removeEventListener(GeolocationEvent.UPDATE, onTravel);

var xml:XML = XML(event.currentTarget.data);

loader = new Loader();

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);

loader.load(new URLRequest(xml));

}

function onLoaded(event:Event):void {

event.currentTarget.removeEventListener(Event.COMPLETE, onLoaded);

this.addChild(event.currentTarget.content);

}

You should see a map of where you are currently located.

The Google Static Maps API


The Google Static Maps API (http://code.google.com/apis/maps/documentation/staticmaps/) offers more features than the Yahoo! product, but at the time of this writing, it enforces a rule whereby static maps can only be displayed as browser content unless you purchase a Google Maps API Premier license. An AIR application is not considered browser content. Read the terms carefully before developing a commercial product using this API.

With this service, a standard HTTP request returns an image with the settings of your choice.

The required

Return Main Page Previous Page Next Page

®Online Book Reader