Google has very good map feature, Using that you can search for the places, you can get the directions, you can find interested places in the area, you can share your photos on the maps etc. All these you must have seen in the browser page. This might be developed using html and JavaScript.
Now it is possible to have all above features in the flash using Google Maps API for Flash. Thanks to Google for providing free API to deal with maps. Believe me; it is very fast and so accurate. I really love it. Apart from the performance feature, using Google Maps API for Flash is so easy. You will learn that in this tutorial.
I will not explain any flash related features in this tutorial, to execute this tutorial one need to have Flash Action Script 3.0 (AS3.0) knowledge.
This tutorial is basically divided into two parts, first part will explain about setting up the Google Maps API in Flash CS3. (I am not using Flash CS5 for this tutorial, as my trial period is expired). Second part will explain about the integrating the Google Map API.
In second part we will cover following features of the Google Maps API.
Set the Map to particular location.
Zoom feature.
Setting Markers.
To use the Google Maps in the flash you will require API. That you can download from Google web site. In this page you will find the SDK link on the right side.
Download the SDK and extract it. You will find two folders, docs and libs. Look inside the libs folder you will find the SWC file for the Flash(map_1_20.swc) as well as for the Flex(map_flex_1_20.swc).
For this tutorial we are using Flash, Hence we will use the map_1_20.swc file. You need to copy map_1_20.swc file to specific folder at following location to get it listed in the component window in Flash IDE.
C:\Program Files\Adobe\Adobe Flash CS3\{language}\Configuration\Components\Google\
Note: I have created Google Folder so that all Google API will can be placed at same location.
Now if you run your Flash CS3. And check the components windows (Windows > Components). You will find the GoogleMpasLibrary component in Google tag(Refer below image). You have set the Google API in your Flash IDE.
Next you will require to have Google Maps API Key. To sign up for the key, click here and follow the instruction in the website. Once you have the generated key, Store it in the Locker (Just Kidding). Make sure you copy that so that you can use it for different applications.
You are all set to develop a flash application using Google Map API.
Open Flash CS3 IDE to start developing your Google Map Application. Now first thing, Open new Flash AS3 document. As a good practice, I always save that to my desire location. So first save the document and Name it as you like. I am naming it with GoogleMapExample.fla. Now next step is to utilize the Google Map API. To use the Google Map API open Components window and drop GoogleMapsLibrary to your stage. You will find only blue bounding box. Don’t worry, API will not show that bounding box it will handle it properly.
Now as we have set the API for our application, It’s time to write some code. I have written following code to complete this application. Don’t worry I have added comments for each statement in the code.
import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.controls.ZoomControl;
import com.google.maps.overlays.*;
import com.google.maps.MapEvent;
import com.google.maps.MapMouseEvent;
import com.google.maps.InfoWindowOptions;
//Map instance
var map:Map = new Map();
//Setting Google Map API key
map.key = "ENTER your GOOGLE MAP KEY";
//Setting Sensor value, Set this true when you are using for GPS.
map.sensor = "false";
//Setting the size of the Map
map.setSize(new Point(stage.stageWidth, stage.stageHeight));
//Adding Listener on Map Raeady.
map.addEventListener(MapEvent.MAP_READY, onMapReady);
//Adding map to the stage.
this.addChild(map);
//Marker instance
var marker:Marker;
//MapEvent.MAP_READY Handler, Fires when Map is ready
function onMapReady(event:Event):void {
//Seting the center of the Map using Latitude and longitude Object, Second parameter is for zoom level
map.setCenter(new LatLng(19.17127500109175, 72.87111282348633), 10);
//Adding Zoom Control to the map
map.addControl(new ZoomControl());
//Setting marker to the center of the Map. using Latitude and longitude Object, Second options is for the Marker option, Here we are getting MarkerOption object by calling custom function.
marker = new Marker(new LatLng(19.17127500109175, 72.87111282348633), createMarkerOption('A'));
//Setting Marker on the Map
map.addOverlay(marker);
//Setting Click hander on the marker to show the information.
marker.addEventListener(MapMouseEvent.CLICK, showInfoWindow)
}
//MapMouseEvent.CLICK Handler, Fires when user click on the Marker
function showInfoWindow(e:MapMouseEvent):void
{
//Opening the Infomation Window,
marker.openInfoWindow(new InfoWindowOptions({contentHTML:"Mumbai , Maharashtra, INDIA "}));
}
//Function to create the Marker Option. Label Paramer.
function createMarkerOption(label:String):MarkerOptions
{
var options:MarkerOptions = new MarkerOptions({
//Stroke style
strokeStyle: {
color: 0x987654
},
//Fill color
fillStyle: {
color: 0x009933,
alpha: 0.8
},
//label
label: label,
//Label Text Format
labelFormat: {
bold: true
},
//Marker Radius
radius: 11,
//Marker Shadow boolean
hasShadow: true,
//Marker Clickable booleam
clickable: true,
//Marker Draggable Boolean
draggable: false
});
return options;
}
In this tutorial I have used Mumbai as a location, You can get your desire location Latitude and longitude by doing following steps.
Step 1: Go to http://maps.google.com
Step 2: Search for your desire place.
Step 3: Paste below code in your browser adress bar.javascript:void(prompt('',gApplication.getMap().getCenter()));
Step 4: This will prompt you with the Latitude and longitude values.
The output will look like this. Click on the image to view the sample application.