1地图联动介绍
是针对Map对象,利用观察者模式,当鼠标进入某一Map时,此Map对象成为主体,其他的Map对其进行观察,若其当前视图范围改变,其他Map随其而动,同时取消对该Map对象的监听。
2具体实现
Map对象具有EXTENT_CHANGE事件
(com.esri.ags.events.ExtentEvent中),监听此事件进行观察。
3具体代码
package Hymn
{
import com.esri.ags.Map;
import com.esri.ags.events.ExtentEvent;
import flash.events.MouseEvent;
publicclass LinkingMethodsClass
{
publicfunction LinkingMethodsClass()
{
MapItems = new Array();
}
privatevar MapItems:Array;
/**
<p>功能:添加需要联动地图对象。</p>
<p>参数:
MapItem Map对象</p>
<p>返回值:无</p>
*/
publicfunction AddMap(MapItem:Map):void
{
for each(var Mapadded:Map inthis.MapItems)
{
if(Mapadded == MapItem)
{
return;
}
}
MapItems.push(MapItem);
AddLinkingStartFunction(MapItem);
}
privatefunction AddLinkingStartFunction(MapItem:Map):void
{
MapItem.addEventListener(MouseEvent.MOUSE_OVER,CheckExtentStartFunction);
}
privatefunction CheckExtentStartFunction(event:MouseEvent):void
{
var MapItem:Map = event.currentTarget as Map;
addLinkingFunction(MapItem);
}
privatefunction addLinkingFunction(MapItem:Map):void
{
MapItem.addEventListener(ExtentEvent.EXTENT_CHANGE,DoLinkingFunction);
}
privatefunction DoLinkingFunction(event:ExtentEvent):void
{
var MapItem:Map = event.currentTarget as Map;
RemoveLinkingFunction();
addLinkingFunction(MapItem);
for each(var MapValue:Map inthis.MapItems)
{
if(MapValue!=MapItem)
{
MapValue.extent = MapItem.extent;
}
}
}
privatefunction RemoveLinkingFunction():void
{
for each(var MapItem:Map inthis.MapItems)
{
MapItem.removeEventListener(ExtentEvent.EXTENT_CHANGE,DoLinkingFunction);
}
}
}
}
4 应用示例
新建Flex工程后,将以下代码粘贴覆盖主运行程序中的代码。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
creationComplete="Intial(event)"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955"
minHeight="600" xmlns:esri="http://www.esri.com/2008/ags">
<fx:Script>
<![CDATA[
import Hymn.LinkingMethodsClass;
import mx.events.FlexEvent;
privatevar MapLikingItem:LinkingMethodsClass
protectedfunction Intial(event:FlexEvent):void
{
// TODO Auto-generated method stub
MapLikingItem = new LinkingMethodsClass();
MapLikingItem.AddMap(this.MapItem1);
MapLikingItem.AddMap(this.MapItem2);
}
]]>
</fx:Script>
<s:VGroup width="100%" height="100%" >
<esri:Map id="MapItem1" >
<esri:ArcGISTiledMapServiceLayer
url="http://hanym-pc/ArcGIS/rest/services/地形图/MapServer"/>
</esri:Map>
<esri:Map id="MapItem2" >
<esri:ArcGISTiledMapServiceLayer
url="http://hanym-pc/ArcGIS/rest/services/地形图/MapServer"/>
</esri:Map>
</s:VGroup>
</s:Application>