(三)检索、添加、显示、隐藏图层及工具导航功能
1、示例源码:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:esri="http://www.esri.com/2008/ags" minWidth="955" minHeight="600" initialize="init()" > <fx:Script> <![CDATA[ import com.esri.ags.Graphic; import com.esri.ags.events.DrawEvent; import com.esri.ags.layers.ArcGISDynamicMapServiceLayer; import com.esri.ags.layers.GraphicsLayer; import com.esri.ags.layers.supportClasses.LayerInfo; import mx.collections.ArrayCollection; import mx.events.ItemClickEvent; [Bindable] private var myLayer:ArcGISDynamicMapServiceLayer; private var myGraphicLayer:GraphicsLayer; protected function init():void{ //为Map控件添加图层 myLayer=new ArcGISDynamicMapServiceLayer(); myLayer.url="http://localhost:8399/arcgis/rest/services/TestUSA/MapServer"; myMap.addLayer(myLayer); //为Map控件添加绘图图层 myGraphicLayer=new GraphicsLayer(); myMap.addLayer(myGraphicLayer); cb_NullCities.addEventListener(Event.CHANGE,cbClickHandler); cb_Highways.addEventListener(Event.CHANGE,cbClickHandler); cb_States.addEventListener(Event.CHANGE,cbClickHandler); cb_Counties.addEventListener(Event.CHANGE,cbClickHandler); } protected function cbClickHandler(event:Event):void{ var cbObj:CheckBox=new CheckBox(); cbObj=CheckBox(event.target); visibleLayer(cbObj.label,cbObj.selected); } //显示隐藏图层 protected function visibleLayer(layerName:String,visible:Boolean):void{ var visibleLayers:ArrayCollection; visibleLayers=myLayer.visibleLayers; var infos:Array=myLayer.layerInfos; //遍历图层 for each(var info:LayerInfo in infos){ if(info.name==layerName){ if(visible){ visibleLayers.addItem(info.id); }else{ var inx:int=visibleLayers.getItemIndex(info.id); if(inx!=-1){ visibleLayers.removeItemAt(inx); } } break; } } } //得地图坐标 protected function mapMouseMoveHandle(event:MouseEvent):void { var lon:Number=myMap.toMapFromStage(event.stageX,event.stageY).x; var lat:Number=myMap.toMapFromStage(event.stageX,event.stageY).y; Lab_X.text="X:"+numberFormater.format(lon); Lab_Y.text="Y:"+numberFormater.format(lat); } protected function tooBar_Clicked(event:ItemClickEvent):void{ //设置点样式 drawTool.markerSymbol=sms; //设置线样式 drawTool.lineSymbol=sls; //设置面样式 drawTool.fillSymbol=sfs; switch(event.index){ case 0: drawTool.deactivate(); //关闭绘图 break; case 1: myMap.zoomIn(); //放大 break; case 2: myMap.zoomOut(); //缩小 break; case 3: drawTool.activate(DrawTool.MAPPOINT); //点 break; case 4: drawTool.activate(DrawTool.MULTIPOINT);//多点 break; case 5: drawTool.activate(DrawTool.LINE); //直线 break; case 6: drawTool.activate(DrawTool.POLYLINE); //线段 break; case 7: drawTool.activate(DrawTool.POLYGON); //多边形 break; case 8: drawTool.activate(DrawTool.FREEHAND_POLYLINE); //自由线 break; case 9: drawTool.activate(DrawTool.EXTENT); //自由面 break; case 10: drawTool.activate(DrawTool.FREEHAND_POLYGON); //矩形 break; case 11: drawTool.activate(DrawTool.CIRCLE); //圆形 break; case 12: drawTool.activate(DrawTool.ELLIPSE); //椭圆 break; case 13: //myGraphicLayer.removeChildAt(1); //删除指定绘图 myGraphicLayer.clear(); break; } } protected function onDrawEnd(event:DrawEvent):void{ //当绘图结束后,将图添加到指定图层 var graphic:Graphic=event.graphic; myGraphicLayer.add(graphic); } ]]> </fx:Script> <fx:Declarations> <mx:NumberFormatter id="numberFormater" precision="2"/> <esri:DrawTool id="drawTool" map="{myMap}" graphicsLayer="{myLayer as GraphicsLayer}" drawEnd="onDrawEnd(event)"/> <!--设置绘图时点、线、面样式--> <esri:SimpleLineSymbol id="sls" style="solid" color="0xFF0000" width="2" alpha="1"/> <esri:SimpleMarkerSymbol id="sms" style="square" color="0xFF0000" size="10" /> <esri:SimpleFillSymbol id="sfs" style="solid" color="0xFF0000" alpha="0.5"/> </fx:Declarations> <s:Group left="0" right="0" top="0" height="106"> <s:CheckBox id="cb_NullCities" x="87" y="52" label="Cities" selected="true"/> <s:CheckBox id="cb_Highways" x="86" y="78" label="Highways" selected="true"/> <s:CheckBox id="cb_States" x="167" y="52" label="States" selected="true"/> <s:CheckBox id="cb_Counties" x="167" y="78" label="Counties" selected="true"/> <s:Label id="Lab_X" x="11" y="56" width="68" text="X"/> <s:Label id="Lab_Y" x="10" y="84" width="68" text="Y"/> <mx:ToggleButtonBar itemClick="tooBar_Clicked(event)"> <mx:dataProvider> <fx:Array> <fx:String>漫游</fx:String> <fx:String>放大</fx:String> <fx:String>缩小</fx:String> <fx:String>点</fx:String> <fx:String>多点</fx:String> <fx:String>线段</fx:String> <fx:String>折线</fx:String> <fx:String>多边形</fx:String> <fx:String>自由线</fx:String> <fx:String>矩形</fx:String> <fx:String>自由面</fx:String> <fx:String>圆形</fx:String> <fx:String>椭圆形</fx:String> <fx:String>清除绘图</fx:String> </fx:Array> </mx:dataProvider> </mx:ToggleButtonBar> </s:Group> <s:Group top="106" width="100%" height="100%"> <esri:Map x="0" y="0" width="100%" height="100%" id="myMap" mouseMove="mapMouseMoveHandle(event)"/> </s:Group> </s:Application>
2、示例图: