ChinaMap.as
package { import event.MapEvent; import flash.display.DisplayObject; import flash.display.DisplayObjectContainer; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.navigateToURL; import flash.external.ExternalInterface; [SWF(width="600",height="500",frameRate="25",backgroundColor="#FFFFFF")] public class ChinaMap extends Sprite { private var mapConfig:Object; private var mapBackGroud:MapBackgound; private var mapArea:MapArea; private var mapXML:XMLList; private var mapTip:MapTip; private var tipShandow:Sprite; private var wrapperFunction:String; public function ChinaMap(){ mapConfig = new Object(); mapConfig.title = loaderInfo.parameters.title; wrapperFunction = loaderInfo.parameters.jsHandler; /*UI*/ var mapLoading:MapLoading = new MapLoading(); addChild(mapLoading); var xmlLoader:URLLoader = new URLLoader(); var xmlAdress:String = (loaderInfo.parameters.xmlurl != null)?loaderInfo.parameters.xmlurl:"data/d.xml"; xmlLoader.addEventListener(Event.COMPLETE,function(e:Event):void{ mapXML = new XML(e.target.data).area; removeChild(mapLoading); drawUI(); }); xmlLoader.load(new URLRequest(xmlAdress)); } private function drawUI():void { mapBackGroud = new MapBackgound(); mapBackGroud.title = (mapConfig.title == null)?"you need set title":mapConfig.title; addChild(mapBackGroud); mapArea = new MapArea(); mapArea.x = mapArea.y = 20; addChild(mapArea); stopAll(mapArea.map); registAction(mapArea.map); tipShandow = new Sprite(); addChild(tipShandow); mapTip = new MapTip(); addChild(mapTip); mapTip.visible = false; } private function registAction(c:DisplayObjectContainer):void { var me:DisplayObject; for(var i:uint = 0; i<c.numChildren; i++) { me = c.getChildAt(i); if(me is MovieClip && me.name != "bg") { me.alpha = 0.5; f:for each(var node:XML in mapXML){ if(node.@id == me.name) { me.alpha = 1; (me as MovieClip).title = node.@title; (me as MovieClip).value = node.@value; (me as MovieClip).navUrl = node.@url; (me as MovieClip).navTarget = node.@target; (me as MovieClip).buttonMode = true; (me as MovieClip).addEventListener(MouseEvent.MOUSE_OVER,mapOverHandler); (me as MovieClip).addEventListener(MouseEvent.MOUSE_OUT,mapOutHandler); (me as MovieClip).addEventListener(MouseEvent.CLICK,mapClipHandler); break f; } } } } function mapOverHandler(e:MouseEvent):void { (e.currentTarget as MovieClip).gotoAndStop(2); showTip((e.currentTarget as MovieClip),(e.currentTarget as MovieClip).value); } function mapOutHandler(e:MouseEvent):void { (e.currentTarget as MovieClip).gotoAndStop(1); if(mouseX < mapTip.x || mouseX > (mapTip.x+mapTip.width) || mouseY < mapTip.y || mouseY > (mapTip.y+mapTip.height)) { hideTip(); } } function mapClipHandler(e:MouseEvent):void { var me:MovieClip = e.currentTarget as MovieClip; if(me.navUrl != null) { navigateToURL(new URLRequest(me.navUrl),me.navTarget); } var clickEvent:MapEvent = new MapEvent(MapEvent.ITEMCLICK,true,true); clickEvent.value = me.name; dispatchEvent(clickEvent); if (ExternalInterface.available) { try { var t:Object = new Object(); t.value = me.name; ExternalInterface.call(wrapperFunction,t); } catch(err:Error) { trace(err); } } } } private function showTip(mc:MovieClip,t:String):void { mapTip.addEventListener(Event.ENTER_FRAME,moveTip); mapTip.visible = true; mapTip.t.htmlText = t; } private function hideTip():void { mapTip.visible=false; mapTip.t.text=""; mapTip.removeEventListener(Event.ENTER_FRAME,moveTip); } private function moveTip(e:Event):void { e.currentTarget.x = mouseX+10; e.currentTarget.y = mouseY+10; if((e.currentTarget.x+e.currentTarget.width) > stage.stageWidth) { e.currentTarget.x = stage.stageWidth - e.currentTarget.width; } } private function stopAll(c:DisplayObjectContainer):void { var me:DisplayObject; for(var i:uint = 0; i<c.numChildren; i++) { me = c.getChildAt(i); if(me is MovieClip) { (me as MovieClip).stop(); } } } } }
ChinaMapDemo.as
package { import event.MapEvent; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; [SWF(width="600",height="500",frameRate="25",backgroundColor="#FFFFFF")] public class ChinaMapDemo extends Sprite { public function ChinaMapDemo() { super(); var mapLoader:Loader = new Loader(); mapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,init); mapLoader.load(new URLRequest("ChinaMap.swf")); addChild(mapLoader); } private function init(e:Event):void { e.currentTarget.content.loaderInfo.parameters.title = "中国地图"; var demoMap:ChinaMap = e.currentTarget.content as ChinaMap; demoMap.addEventListener(MapEvent.ITEMCLICK,clickHandler); } private function clickHandler(e:MapEvent):void { trace(e.value); } } }
data/d.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<area id="beijing" title="北京" value="北京,人口:100万,面积:380万平方公里,主要交通工具:自行车" url="www.baidu.com" target="_blank"/>
<area id="tianjin" title="天津" value="天津" url="test1.html" target="_blank"/>
<area id="xinjiang" title="新疆" value="<font color="#990000" size="18">这是HTML字体</font>"/>
<area id="xizang" title="西藏" value="西藏" url="test3.html" target="_blank"/>
<area id="qinghai" title="青海" value="青海" url="test.html" target="_blank"/>
<area id="gansu" title="甘肃" value="甘肃" url="test.html" target="_blank"/>
<area id="neimenggu" title="内蒙古" value="内蒙古" url="test.html" target="_blank"/>
<area id="ningxia" title="宁夏" value="宁夏" url="test.html" target="_blank"/>
<area id="shanxi" title="山西" value="山西" url="test.html" target="_blank"/>
<area id="liaoning" title="辽宁" value="辽宁" url="test.html" target="_blank"/>
<area id="jilin" title="吉林" value="吉林" url="test.html" target="_blank"/>
<area id="heilongjiang" title="黑龙江" value="黑龙江" url="test.html" target="_blank"/>
<!--
<area id="hebei" title="河北" value="河北" url="test.html" target="_blank"/>
<area id="shandong" title="山东" value="山东" url="test.html" target="_blank"/>
-->
<area id="henan" title="河南" value="河南" url="test.html" target="_blank"/>
<area id="shannxi" title="陕西" value="陕西" url="test.html" target="_blank"/>
<area id="sichuan" title="四川" value="四川" url="test.html" target="_blank"/>
<area id="chongqing" title="重庆" value="重庆" url="test.html" target="_blank"/>
<area id="hubei" title="湖北" value="湖北" url="test.html" target="_blank"/>
<area id="anhui" title="安徽" value="安徽" url="test.html" target="_blank"/>
<area id="jiangsu" title="江苏" value="江苏" url="test.html" target="_blank"/>
<area id="shanghai" title="上海" value="上海" url="test.html" target="_blank"/>
<area id="zhejiang" title="浙江" value="浙江" url="test.html" target="_blank"/>
<area id="fujian" title="福建" value="福建" url="test.html" target="_blank"/>
<area id="taiwan" title="台湾" value="台湾" url="test.html" target="_blank"/>
<area id="jiangxi" title="江西" value="江西" url="test.html" target="_blank"/>
<area id="hunan" title="湖南" value="湖南" url="test.html" target="_blank"/>
<area id="guizhou" title="贵州" value="贵州" url="test.html" target="_blank"/>
<area id="guangxi" title="广西" value="广西" url="test.html" target="_blank"/>
<area id="guangdong" title="广东" value="广东" url="test.html" target="_blank"/>
<area id="xianggang" title="香港" value="香港" url="test.html" target="_blank"/>
<area id="hainan" title="海南" value="海南" url="test.html" target="_blank"/>
<area id="yunnan" title="云南" value="云南" url="test.html" target="_blank"/>
</data>