identifyTask练习
生活随笔
收集整理的這篇文章主要介紹了
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" minWidth="955" minHeight="600" xmlns:esri="http://www.esri.com/2008/ags"><fx:Script><![CDATA[import com.esri.ags.Graphic;import com.esri.ags.events.MapMouseEvent;import com.esri.ags.geometry.Geometry;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.rpc.AsyncResponder;[Bindable]private var lastIdentifyResultGraphic:Graphic;private function mapClickHandler(event:MapMouseEvent):void{clickGraphicsLayer.clear();var identifyParams:IdentifyParameters = new IdentifyParameters();identifyParams.returnGeometry = true;identifyParams.tolerance = 3;identifyParams.width = myMap.width;identifyParams.height = myMap.height;identifyParams.geometry = event.mapPoint;identifyParams.mapExtent = myMap.extent;identifyParams.spatialReference = myMap.spatialReference;var clickGraphic:Graphic=new Graphic(event.mapPoint,SMS);clickGraphicsLayer.add(clickGraphic);identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, clickGraphic));}private function myResultFunction(results:Array,clickGraphic:Graphic=null):void{if(results&&results.length>0){var result:IdentifyResult=results[0];var resultGraphic:Graphic=result.feature;switch (resultGraphic.geometry.type){case Geometry.MAPPOINT:{resultGraphic.symbol = SMS;break;}case Geometry.POLYLINE:{resultGraphic.symbol = SLS;break;}case Geometry.POLYGON:{resultGraphic.symbol = SFS;break;}}lastIdentifyResultGraphic=resultGraphic;clickGraphic.symbol=new InfoSymbol();clickGraphic.attributes=resultGraphic.attributes;}}private function myFaultFunction(error:Object, clickGraphic:Graphic = null):void{Alert.show(String(error), "Identify Error");}]]></fx:Script><fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --><esri:SimpleMarkerSymbol id="SMS" color="0xFF0000" size="12" style="circle"/><esri:SimpleLineSymbol id="SLS" color="0xFF0000" width="2" alpha="1" style="solid"/><esri:SimpleFillSymbol id="SFS" color="0xFF0000"/><esri:IdentifyTask id="identifyTask" concurrency="last"url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"/></fx:Declarations><esri:Map id="myMap"mapClick="mapClickHandler(event)"openHandCursorVisible="false"><esri:extent><esri:WebMercatorExtent minlon="-120" minlat="30" maxlon="-100" maxlat="50"/></esri:extent><esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/><esri:GraphicsLayer graphicProvider="{lastIdentifyResultGraphic}"/><esri:GraphicsLayer id="clickGraphicsLayer"/></esri:Map></s:Application>
該練習涉及到的知識點: 1 先定義了一個Identify Task對象,如下面代碼: <esri:IdentifyTask id="identifyTask"
identifyComplete="identifyCompleteHandler(event)"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_States
CitiesRivers_USA/MapServer"/> 其中,identifyComplete事件綁定IdentifyCompleteHandler(event)函數,即IdentifyTask對象設置完成之后實現該函數,url指定IdentifyTask服務地址。但在練習中沒有用到該綁定事件。 2 執行Identify,需要定義一個IdentifyParameters對象,把所需要的參數進行傳遞,其中tolerance是容差半徑,geometry是用來做Identify的幾何對象。此外還定義了clickGraphic用來設置鼠標點擊出現的符號 identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, clickGraphic)); 這句代碼主要用來向REST請求Identify服務,并設置了請求服務成功則調用myResultFunction函數,失敗則調用myFaultFunction函數(這就是前面定義IdentifyTask時不設置identifyComplete事件的原因) 3 接下來主要是編寫myResultFunction函數 var result:IdentifyResult=results[0];var resultGraphic:Graphic=result.feature; 用IdentifyResult來獲取feature并賦予resultGraphic對象,并根據該對象的幾何性質賦予渲染符號;最后把resultGraphic的屬性賦給clickGraphic并用InfoSymbol顯示出來另外需要注意的是:<esri:GraphicsLayer graphicProvider="{lastIdentifyResultGraphic}"/>lastIdentifyResultGraphic=resultGraphic;實驗設置了一個全局變量作為GraphicLayer的數據源。
identifyComplete="identifyCompleteHandler(event)"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_States
CitiesRivers_USA/MapServer"/> 其中,identifyComplete事件綁定IdentifyCompleteHandler(event)函數,即IdentifyTask對象設置完成之后實現該函數,url指定IdentifyTask服務地址。但在練習中沒有用到該綁定事件。 2 執行Identify,需要定義一個IdentifyParameters對象,把所需要的參數進行傳遞,其中tolerance是容差半徑,geometry是用來做Identify的幾何對象。此外還定義了clickGraphic用來設置鼠標點擊出現的符號 identifyTask.execute(identifyParams, new AsyncResponder(myResultFunction, myFaultFunction, clickGraphic)); 這句代碼主要用來向REST請求Identify服務,并設置了請求服務成功則調用myResultFunction函數,失敗則調用myFaultFunction函數(這就是前面定義IdentifyTask時不設置identifyComplete事件的原因) 3 接下來主要是編寫myResultFunction函數 var result:IdentifyResult=results[0];var resultGraphic:Graphic=result.feature; 用IdentifyResult來獲取feature并賦予resultGraphic對象,并根據該對象的幾何性質賦予渲染符號;最后把resultGraphic的屬性賦給clickGraphic并用InfoSymbol顯示出來另外需要注意的是:<esri:GraphicsLayer graphicProvider="{lastIdentifyResultGraphic}"/>lastIdentifyResultGraphic=resultGraphic;實驗設置了一個全局變量作為GraphicLayer的數據源。
轉載于:https://www.cnblogs.com/gisak/archive/2011/05/06/2038579.html
總結
以上是生活随笔為你收集整理的identifyTask练习的全部內容,希望文章能夠幫你解決所遇到的問題。