Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [57]

By Root 2481 0
the Google Maps API for Flash (http://code.google.com/apis/maps/documentation/flash/), Flex developers can embed Google maps in Flash applications. Sign up for a Google Maps API key and download the Flash SDK. Use version 20 or up (map_1_20.swc or map_flex_1_20.swc), as version 19 has a known issue with ResizeEvent. Add the path to the .swc file in the library path and set Default Linkage to “Merged into code”. As you will see, this API offers a wealth of options that are easy to implement.

NOTE

To set the library path in Flash Professional, go to File→Publish Settings. Click the tool icon next to Script. Select the Library Path tab and click the Flash icon to navigate to the .swc file. Once you’ve imported the file, change Default Linkage to “Merged into code”.

In Flash Builder, right-click your project, go to Properties→ActionScript Build Path, and click Add SWC to navigate to the .swc file. “Merged into code” is the default setting.

Create a Map object as well as key and url properties. Entering both your API key and the site URL you submitted when you applied for the key is required even though you are not displaying the map in your website but rather as a standalone Android application.

The sensor parameter, also required, states whether you use a GPS sensor. It needs to be a string, not a boolean. The map size, defined by setSize, is set dynamically to the dimensions of the stage.

When the map is ready, the geolocation listener is set up. After the first update is received, the setCenter function is called with location, zoom level, and the type of map to use. Finally, the zoom control is added. Figure 10-4 shows the result:

Figure 10-4. My current location

import com.google.maps.Map;

import com.google.maps.MapEvent;

import com.google.maps.MapType;

import com.google.maps.LatLng;

import flash.geom.Point;

import flash.sensors.Geolocation;

import flash.events.GeolocationEvent;

import com.google.maps.controls.ZoomControl;

const KEY:String = YOUR_API_KEY;

const SITE:String = YOUR_SITE;

var map:Map;

var geolocation:Geolocation;

map = new Map();

map.key = KEY;

map.url = SITE;

map.sensor = "true";

map.setSize(new Point(stage.stageWidth, stage.stageHeight));

map.addEventListener(MapEvent.MAP_READY, onMapReady);

function onMapReady(event:MapEvent):void {

geolocation = new Geolocation();

geolocation.addEventListener(GeolocationEvent.UPDATE, onTravel);

addChild(map);

}

function onTravel(event:GeolocationEvent):void {

geolocation.removeEventListener(GeolocationEvent.UPDATE, onTravel);

map.setCenter(new LatLng(event.latitude, event.longitude),

18, MapType.NORMAL_MAP_TYPE);

map.addControl(new ZoomControl());

}

Add a marker as landmarks and navigation control. Here the marker is customized to have a shadow, a blue color, and a defined radius. When you click on it, an information window opens with text content:

import com.google.maps.MapMouseEvent;

import com.google.maps.overlays.Marker;

import com.google.maps.overlays.MarkerOptions;

import com.google.maps.InfoWindowOptions;

var options:Object = {hasShadow:true,

fillStyle: new FillStyle({color:0x0099FF, alpha:0.75}),

radius:12

};

var marker:Marker =

new Marker(new LatLng(45.7924, 15.9696), new MarkerOptions(options));

marker.addEventListener(MapMouseEvent.CLICK, markerClicked);

map.addOverlay(marker);

function markerClicked(event:MapMouseEvent):void {

event.currentTarget.openInfoWindow

(new InfoWindowOptions({content:"hello"});

}

Styled Maps support


In October 2010, Google announced support for Styled Maps on Flash, included in Flash SDK version 20 and up (see http://code.google.com/apis/maps/documentation/flash/maptypes.html#StyledMaps). This addition gives you control over color scheme and customization of markers and controls. It makes your map look more unique or match your brand and design. You can also write or draw over the map. The Google Geo Developers Blog (http://googlegeodevelopers.blogspot.com/2010/10/five-great-styled-maps-examples.html)

Return Main Page Previous Page Next Page

®Online Book Reader