Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [56]

By Root 2527 0
parameters are as follows:

center for location as an address or latitude/longitude (not required with marker)

zoom from 0 for the Earth to 21 for a building (not required with marker)

size (up to 640×640 pixels)

sensor (with or without use of GPS locator)

The maximum image size is 640×640. Unless you scale the image up in size, it will not fill the screen on most Android devices. Optional parameters are mobile, format, maptype, language, markers, visible, and path.

The following example requests a 480×640 image of Paris centered on the Eiffel Tower:

import flash.display.Loader;

import flash.net.URLRequest;

import flash.events.Event;

const GOOGLE_URL:String = "http://maps.google.com/maps/api/staticmap?"

function loadStaticImage():void {

var request:String = "center=Eiffel+Tower,Paris,France

&zoom=16&size=480x640&sensor=false";

var loader:Loader = new Loader();

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

loader.load(new URLRequest(GOOGLE_URL + request));

}

function imageLoaded(event:Event):void {

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

addChild(event.currentTarget.content);

}

You should see on your screen the map of Paris, as shown in Figure 10-3.

Let’s make another request using a dynamic location. The sensor parameter is now set to true and we are adding the mobile parameter. The image size is also dynamic, choosing whichever value is the smallest between Google restricted values and our stage size. And we are now using the hybrid version of the maptype:

import flash.display.Loader;

import flash.net.URLRequest;

import flash.sensors.Geolocation;

const GOOGLE_URL:String = "http://maps.google.com/maps/api/staticmap?"

var geolocation:Geolocation = new Geolocation();

geolocation.addEventListener(GeolocationEvent.UPDATE, onTravel);

function onTravel(event:GeolocationEvent):void {

geolocation.removeEventListener(GeolocationEvent.UPDATE, onTravel);

loadStaticImage(event.latitude, event.longitude);

}

function loadStaticImage(lat:Number, long:Number):void {

var width:int = Math.min(640, stage.stageWidth);

var height:int = Math.min(640, stage.stageHeight);

var request:String = "center=" + lat + "," + long +

"&zoom=15

&size=" + width + "x" + height +

"&maptype=hybrid&mobile=true&sensor=true";

var loader:Loader = new Loader();

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

loader.load(new URLRequest(GOOGLE_URL + request));

}

function imageLoaded(event:Event):void {

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

addChild(event.currentTarget.content);

}

Figure 10-3. Map of Paris centered on the Eiffel Tower

The center parameter can be substituted for one or multiple markers. A marker can be given a color and a label, or it can be customized. And we are now using the road map version of the maptype:

function loadStaticImage(lat:Number, long:Number):void {

var width:int = Math.min(640, stage.StageWidth);

var height:int = Math.min(640, stage.StageHeight);

var request:String = "markers=size:large|color:blue|label:L|"

+ lat + "," + long

+ "&zoom=16"

+ &size=" + width + "x" + height +

"&maptype=roadmap&mobile=true&sensor=true";

var loader:Loader = new Loader();

addChild(loader);

loader.load(new URLRequest(googleURL + request));

}

This is again a low-impact but limited solution. The user only sees an image that cannot be scaled and is only given basic information. Let’s now look at using actual maps.

Dynamic Maps


Yahoo! and Google both provide well-documented AS3 libraries for use with their map APIs. The only restriction for both is a maximum of 50,000 uses per day.

NOTE

Maps are slow to initialize. Create placeholder art to display instead of the default gray rectangle, and display an attractive loading animation. Do not forget to remove the art after the map appears. Any art under the map would slow down its performance.

The Google Maps API for Flash


With

Return Main Page Previous Page Next Page

®Online Book Reader