(四)使用IdentifyTask查询图层属性
一、完整代码
<?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"> <fx:Declarations> <esri:DrawTool id="drawTool" map="{myMap}" graphicsLayer="{myLayer as GraphicsLayer}" drawEnd="onDrawEnd(event)"/> <esri:IdentifyTask id="identifyTask" url="{serverUrl}"/> <!-- Symbol for where the user clicked --> <esri:SimpleMarkerSymbol id="clickPtSym" color="0xFF0000" size="12" style="x"/> <!-- Symbol for Identify Result as Polyline --> <esri:SimpleLineSymbol id="slsIdentify" width="2" alpha="1" color="0x00FF00" style="solid"/> <!-- Symbol for Identify Result as Point --> <esri:SimpleMarkerSymbol id="smsIdentify" color="0x00FF00" size="15" style="diamond"/> <esri:SimpleFillSymbol id="sfsIdentify"/> </fx:Declarations> <fx:Script> <![CDATA[ import com.esri.ags.Graphic; import com.esri.ags.events.DrawEvent; import com.esri.ags.geometry.Geometry; import com.esri.ags.geometry.MapPoint; import com.esri.ags.geometry.Polygon; import com.esri.ags.geometry.Polyline; import com.esri.ags.symbols.InfoSymbol; import com.esri.ags.tasks.supportClasses.IdentifyParameters; import com.esri.ags.tasks.supportClasses.IdentifyResult; import mx.controls.Alert; import mx.controls.DataGrid; import mx.events.ItemClickEvent; import mx.rpc.AsyncResponder; [Bindable] private var serverUrl:String="http://localhost:8399/arcgis/rest/services/TestUSA/MapServer"; [Bindable] private var lastIdentifyResultGraphic:Graphic; protected function toolBar_Clicked(event:ItemClickEvent):void{ switch(event.index){ case 0: drawTool.deactivate(); break; case 1: drawTool.activate(DrawTool.EXTENT); break; } } protected function onDrawEnd(event:DrawEvent):void{ var identifyParams:IdentifyParameters=new IdentifyParameters(); identifyParams.returnGeometry=true; identifyParams.tolerance=3; identifyParams.width = myMap.width; identifyParams.height = myMap.height; identifyParams.geometry=event.graphic.geometry; identifyParams.mapExtent=myMap.extent; /** * 固定查询第一个图层,即高速公路,可以使用下拉框进行选择其它图层(需要遍历所有图层),查询多个图层 * identifyParams.layerOption=IdentifyParameters.LAYER_OPTION_VISIBLE; 所有可见图层 * LAYER_OPTION_ALL; 所有图层 * LAYER_OPTION_TOP; 最上层 * */ var layers:Array=new Array(); layers.push(1); identifyParams.layerIds=layers; identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, event.graphic)); } //绘图正常返回后 private function myResultFunction(results:Array, clickGraphic:Graphic = null):void{ resultGraphicsLayer.clear(); if(results && results.length>0){ var attriList:Array=new Array(); for each (var identifyResult:IdentifyResult in results) { var resultGraphic:Graphic = identifyResult.feature; //设置线样试,本示例只查询了公路,如果有多个,可以判断点,面进行不同设置 resultGraphic.symbol = slsIdentify; //装载一行所有属性 //attriList.push(resultGraphic.attributes); //装载个别属性 attriList.push({"公路名称":resultGraphic.attributes.TYPE,"公路长度":resultGraphic.attributes.LENGTH}); resultGraphicsLayer.add(resultGraphic); } var sg:DataGrid=new DataGrid(); sg.width=200; sg.dataProvider=attriList; myMap.infoWindowContent=sg; myMap.infoWindow.label="高速公路信息"; myMap.infoWindow.show(getGeomCenter(resultGraphic)); } } private function myFaultFunction(error:Object, clickGraphic:Graphic = null):void { Alert.show(String(error), "Identify Error"); } //根据所绘图层,得显示气泡的点位置 private function getGeomCenter(graphic:Graphic):MapPoint { var pt:MapPoint; switch (graphic.geometry.type) { case Geometry.MAPPOINT: { pt = graphic.geometry as MapPoint; break; } case Geometry.POLYLINE: { const pl:Polyline = graphic.geometry as Polyline; const pathCount:Number = pl.paths.length; const pathIndex:int = int((pathCount / 2) - 1); const midPath:Array = pl.paths[pathIndex]; const ptCount:Number = midPath.length; const ptIndex:int = int((ptCount / 2) - 1); pt = pl.getPoint(pathIndex, ptIndex); break; } case Geometry.POLYGON: { const poly:Polygon = graphic.geometry as Polygon; pt = poly.extent.center; break; } } return pt; } ]]> </fx:Script> <mx:ToggleButtonBar itemClick="toolBar_Clicked(event)"> <mx:dataProvider> <fx:Array> <fx:String>漫游</fx:String> <fx:String>矩形查询</fx:String> </fx:Array> </mx:dataProvider> </mx:ToggleButtonBar> <esri:Map x="0" y="86" width="100%" height="100%" id="myMap"> <esri:ArcGISDynamicMapServiceLayer url="{serverUrl}" id="myLayer"/> <esri:GraphicsLayer id="resultGraphicsLayer"/> <esri:GraphicsLayer id="clickGraphicsLayer"/> </esri:Map> </s:Application>
二、效果图