What are we building:

Download the source code here Download

What is required?

  1. Install google maps api for flash ( Emanuele Feronato tutorial )
  2. Flash IDE ( Ability to compile & export apk for android ) Im currently using flash CS5
  3. AS3 knowledge ( duh )

once you install google maps api for flash, navigate to the components and drag out a GoogleMapsLibrary component to the library.

Under the document class enter Main ( the external AS3 file ) which we’ll be having the code in.

Save the Fla file and in the same directory create an AS file and save it as Main.as, Copy & Paste the following code in Main.as :

package  {

	//Flash imports
	import flash.display.MovieClip;
	import flash.sensors.Geolocation;
	import flash.events.GeolocationEvent;
	import flash.geom.Point;
	import flash.events.Event;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextField;
	import flash.text.TextFormat;
	//Google maps imports
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.MapType;
	import com.google.maps.controls.ZoomControl;
	import com.google.maps.controls.MapTypeControl;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.MapMouseEvent;
	import com.google.maps.InfoWindowOptions;

	public class Main extends MovieClip {
		//declare map
		public var map:Map;
		//declare geilocation
		private var myLoc:Geolocation;
		//declare marker
		private var marker:Marker;
		private var myText:TextField;
		public var format:TextFormat;

		public function Main() {
			// constructor code

			super();
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			addEventListener(Event.ADDED_TO_STAGE, init);

		}//main ends

		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);

			map = new Map();
			map.key = "ABQIAAAAYE0bGtlAc2aNHkbZAX8d5xTtk0dF-T8JMCdCfbxeilM7kTvGfhSLRtD2VK3FR9ecXk7jWpMWxZuZvw";
			map.sensor = "false";
			map.url = "http://www.zainals.com";
			map.setSize(new Point (stage.stageWidth, stage.stageHeight));
			addChildAt( map, 0 ) ;
			map.addEventListener(MapEvent.MAP_READY, onMapReady);

			txtField();

		}//init ends 

		private function onMapReady(e:MapEvent):void
		{
				map.addControl (new ZoomControl()  );
				map.addControl (new MapTypeControl() );
				map.setCenter( new LatLng( 26.116602, 50.482178), 14, MapType.SATELLITE_MAP_TYPE);

				if(Geolocation.isSupported) {

					myLoc = new Geolocation();
					myLoc.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
					myLoc.setRequestedUpdateInterval(100);

				}

		}//onMapReady Ends

		private function onGeoUpdate(e:GeolocationEvent):void
		{
				map.setCenter( new LatLng( e.latitude, e.longitude), 18, MapType.SATELLITE_MAP_TYPE);
				setMarker( new LatLng(e.latitude, e.longitude));

		}//onGeoUpdate ends

		private function txtField():void
		{
			format = new TextFormat();
			format.size = 20;
			format.align = "center"
			format.font = "Verdana"
			format.color = 0xffffff;

			myText = new TextField();
			myText.width = stage.stageWidth
			myText.height = 40;
			myText.background = true;
			myText.backgroundColor = 0x000000;
			myText.y= 0;
			myText.text = "initializing GMAPS";
			addChild(myText);
			myText.setTextFormat(format);
		}//txtField

		private function setMarker(p:LatLng):void
		{
			if (marker){

				marker.setLatLng(p);

			}else {

			marker = new Marker(p);
			map.addOverlay(marker);
			marker.addEventListener(MapMouseEvent.CLICK, showInfo);

			}//if else ends

			myText.htmlText = p.toString();
			myText.setTextFormat(format);

		}//setMarker Ends

		private function showInfo(e:MapMouseEvent):void
		{

		var markerContent:String = marker.getLatLng().toString();
		marker.openInfoWindow( new InfoWindowOptions({contentHTML: markerContent}));

		}//showinfo ends

	}//class ends

}//package ends

dont forget to set application permissions: AIR for Android settings > Permissions & tick:

  • Internet
  • Access fine location