15Feb

[AS 3] Actionscript 3 and Yahoo Maps without Flex

No comments

Fortunately I came across this site (zeuslabs) that has a hack to allow the use of Yahoo! Maps API. Their site provides excellent documentation of this implementation. I wrote up a class document that can viewed below.

Here is the class file I modified a little:

package {
    import flash.display.Sprite;
	import flash.display.Loader;
	import flash.display.DisplayObject;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.system.ApplicationDomain;

    public class YahooMaps extends Sprite
    {
        private static const APP_ID:String = "MyYahooAppID";  

		private var YahooMap:Class;
		private var YahooMapEvent:Class;
		private var LatLon:Class;

		public var map:Object;

        public function YahooMaps()
        {
			this.stage.align = "topLeft";
			this.stage.scaleMode = "noScale";

			// library.swf has the classes we need, so let's load it in
			var mapsLoader:Loader = new Loader();
			mapsLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, mapsLoaderCompleteHandler);
			mapsLoader.load(new URLRequest("/path/to/library.swf"));
        }

		private function mapsLoaderCompleteHandler(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE, mapsLoaderCompleteHandler);

			// save each of the classes
			var appDomain:ApplicationDomain = event.target.applicationDomain;
			YahooMap = appDomain.getDefinition("com.yahoo.maps.api.YahooMap") as Class;
			YahooMapEvent = appDomain.getDefinition("com.yahoo.maps.api.YahooMapEvent") as Class;
			LatLon = appDomain.getDefinition("com.yahoo.maps.api.core.location.LatLon") as Class;

			this.map = new YahooMap(APP_ID, stage.stageWidth, stage.stageHeight);
			this.map.addPanControl();
			this.map.addZoomWidget();
			this.map.addTypeWidget();

			this.map.addEventListener(YahooMapEvent.MAP_INITIALIZE, mapInitializeHandler);

			this.addChild(DisplayObject(this.map));
		}

		private function mapInitializeHandler(event:Event):void
		{
			this.map.removeEventListener(YahooMapEvent.MAP_INITIALIZE, mapInitializeHandler);
			this.map.zoomLevel = 2;
			this.map.centerLatLon = new LatLon(44.445914,-73.168602);
		}
    }
}

Share/Save/Bookmark

Friday, February 15th, 2008 at 10:54 am and is filed under Actionscript 3, Yahoo! Components. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply