ArcGIS For Flex学习之Mapping---Select and zoom
Tips:这个例子说明如何在图层中选择或反选几何对象,当点击地图时,触发mapClick事件,激活QueryTask,这里的Query中的query.geometry = event.mapPoint;利用鼠标点击的点来查找包括该选择点的Polygon,然后将查询到的几何要素添加到GraphicsLayer中并放大一个级别;同时,再次点击选择的要素时,触发unselectGraphic事件,并从GraphicsLayer中移除该要素。具体效果图及代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 3 xmlns:s="library://ns.adobe.com/flex/spark" 4 xmlns:esri="http://www.esri.com/2008/ags" 5 pageTitle="Select and Zoom"> 6 7 <fx:Script> 8 <![CDATA[ 9 import com.esri.ags.Graphic; 10 import com.esri.ags.events.MapMouseEvent; 11 import com.esri.ags.events.QueryEvent; 12 import com.esri.ags.geometry.Extent; 13 import com.esri.ags.utils.GraphicUtil; 14 15 import mx.collections.ArrayCollection; 16 import mx.collections.ArrayList; 17 18 private var mapClickToggler:Boolean = true; 19 20 private function mapClickHandler(event:MapMouseEvent):void 21 { 22 if (mapClickToggler) 23 { 24 query.geometry = event.mapPoint; 25 queryTask.execute(query); 26 } 27 } 28 29 private function executeCompleteHandler(event:QueryEvent):void 30 { 31 for each (var myGraphic:Graphic in event.featureSet.features) 32 { 33 myGraphic.symbol = mySymbol; 34 myGraphic.addEventListener(MouseEvent.CLICK, unselectGraphic); 35 // myGraphic.addEventListener(MouseEvent.ROLL_OVER, toggleMapClick); 36 // myGraphic.addEventListener(MouseEvent.ROLL_OUT, toggleMapClick); 37 myGraphicsLayer.add(myGraphic); 38 } 39 zoomToGraphics(); 40 } 41 42 private function zoomToGraphics():void 43 { 44 var graphicProvider:ArrayCollection = myGraphicsLayer.graphicProvider as ArrayCollection; 45 var graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(graphicProvider.toArray()); 46 47 if (graphicsExtent) 48 { 49 myMap.extent = graphicsExtent; 50 51 // make sure the whole extent is visible 52 if (!myMap.extent.contains(graphicsExtent)) 53 { 54 myMap.level--; 55 } 56 } 57 } 58 59 private function unselectGraphic(event:MouseEvent):void 60 { 61 myGraphicsLayer.remove(event.currentTarget as Graphic); 62 zoomToGraphics(); 63 } 64 65 private function toggleMapClick(event:MouseEvent):void 66 { 67 mapClickToggler = !mapClickToggler; 68 } 69 ]]> 70 </fx:Script> 71 72 <fx:Declarations> 73 <esri:QueryTask id="queryTask" 74 executeComplete="executeCompleteHandler(event)" 75 url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" 76 useAMF="false"/> 77 <esri:Query id="query" 78 outSpatialReference="{myMap.spatialReference}" 79 returnGeometry="true"/> 80 <esri:SimpleFillSymbol id="mySymbol" 81 alpha="0.7" 82 color="#D3D1D1" 83 > 84 </esri:SimpleFillSymbol> 85 </fx:Declarations> 86 87 <s:controlBarContent> 88 <s:RichText width="100%"> 89 This sample demonstrates how to select or deselect a graphic 90 in a graphics layer. This is accomplished by using a QueryTask to 91 add features to a graphics layer, or remove graphics from the graphics 92 layer by clicking on that graphic. The GraphicUtil class is also used 93 in this sample to get the extent of all the graphics in the graphics 94 layer and adjust the map extent and scale based upon if the features 95 are contained within the current extent. 96 Click on a state to select or unselect it. The map will zoom to current selection. 97 </s:RichText> 98 </s:controlBarContent> 99 100 <esri:Map id="myMap" mapClick="mapClickHandler(event)"> 101 <esri:extent> 102 <esri:Extent xmin="-13901000" ymin="3292000" xmax="-8812000" ymax="6154000"> 103 <esri:SpatialReference wkid="102100"/> 104 </esri:Extent> 105 </esri:extent> 106 <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/> 107 <esri:ArcGISDynamicMapServiceLayer url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer" visibleLayers="{new ArrayList([5])}"/> 108 <esri:GraphicsLayer id="myGraphicsLayer"/> 109 </esri:Map> 110 111 </s:Application>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。