arcgis api for flex 開發入門(六)identify
identify 是GIS中比較常用的工具之一,在arcgis api for flex中esri為我們提
供了一個Identify Task來輕鬆完成identify 的功能。
首先,還是使用<esri:IdentifyTask>標籤來建立一個Identify Task。
<!-- Identify Task -->
<esri:IdentifyTask id="identifyTask"
identifyComplete="identifyCompleteHandler(event)"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Special
ty/ESRI_StatesCitiesRivers_USA/MapServer"/>
當identifyTask執行完畢的時候響應identifyComplete訊息,我們就可以把
identify的結果做一些處理,比如添加到Graphic layer 上。
在執行identify之前,首先要對identify的參數設定一下,我們需要一個
IdentifyParameters對象。下面的代碼是AS3指令碼的代碼,用來建立
IdentifyParameters和identify執行。
var identifyParams : IdentifyParameters = new IdentifyParameters();
identifyParams.returnGeometry = true;
identifyParams.tolerance = 3;
identifyParams.width = 600;
identifyParams.height = 550;
identifyParams.geometry = geometry;
identifyParams.layerOption =
IdentifyParameters.LAYER_OPTION_ALL;
identifyParams.mapExtent = map.extent;
identifyTask.execute( identifyParams );
其中tolerance是容差半徑
width:Width of the map currently being viewed in pixels.
height : Height of the map currently being viewed in pixels
geometry 是用來做identify的幾何,常用的有點選,矩形選擇,多邊形選擇等
參數設定好了之後,直接調用identifyTask.execute( identifyParams );就ok了
。
那麼我們用來做identify的幾何怎麼來呢,在什麼時候去做Identify呢?
首先回答第一個問題,做identify的幾何我們可以利用第四講中draw控制項使用鼠
標互動來獲得,這也是RIA的特點之一。
那麼在什麼時候做identify呢?
就在做identify的幾何畫完之後做,嘿嘿,等於沒說嘛,當然要在畫完了就做:-D
現在我們就來完成上面的工作
定義一個draw控制項
<esriraw id="drawToolbar" map="{map}"
graphicsLayer="{myGraphicsLayer}" drawEnd="drawEndHandler(event)">
記得添加上drawEnd訊息的響應函數drawEndHandler(event),這個事件會在draw
之後響應。
用as3指令碼實現drawEndHandler和identifyCompleteHandler函數
private function drawEndHandler(eventrawEvent):void
{
var geometry : Geometry = event.geometry;
var identifyParams : IdentifyParameters = new
IdentifyParameters();
identifyParams.returnGeometry = true;
identifyParams.tolerance = 3;
identifyParams.width = 600;
identifyParams.height = 550;
identifyParams.geometry = geometry;
identifyParams.layerOption =
IdentifyParameters.LAYER_OPTION_ALL;
identifyParams.mapExtent = map.extent;
identifyTask.execute( identifyParams );
private function identifyCompleteHandler
(event:IdentifyEvent):void
{
for each (var result:IdentifyResult in
event.identifyResults)
{
myGraphicsLayer.add(result.feature);
}
}Code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:esri="http://www.esri.com/2008/ags"
layout="absolute"
pageTitle="Identify Features on the Map"
>
<mx:Script>
<![CDATA[
import com.esri.ags.Graphic;
import com.esri.ags.events.DrawEvent;
import com.esri.ags.events.IdentifyEvent;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.symbol.Symbol;
import com.esri.ags.tasks.IdentifyParameters;
import com.esri.ags.tasks.IdentifyResult;
import com.esri.ags.toolbars.Draw;
private function drawEndHandler(event:DrawEvent):void
{
var geometry : Geometry = event.geometry;
var identifyParams : IdentifyParameters = new
IdentifyParameters();
identifyParams.returnGeometry = true;
identifyParams.tolerance = 3;
identifyParams.width = 600;
identifyParams.height = 550;
identifyParams.geometry = geometry;
identifyParams.layerOption =
IdentifyParameters.LAYER_OPTION_ALL;
identifyParams.mapExtent = map.extent;
identifyTask.execute( identifyParams );
}
private function identifyCompleteHandler
(event:IdentifyEvent):void
{
for each (var result:IdentifyResult in
event.identifyResults)
{
myGraphicsLayer.add(result.feature);
}
}
]]>
</mx:Script>
<!-- Draw ToolBar -->
<esri:Draw id="drawToolbar" map="{map}"
graphicsLayer="{myGraphicsLayer}" drawEnd="drawEndHandler(event)">
</esri:Draw>
<!-- Identify Task -->
<esri:IdentifyTask id="identifyTask"
identifyComplete="identifyCompleteHandler(event)"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Special
ty/ESRI_StatesCitiesRivers_USA/MapServer"/>
<mx:Panel width="100%" height="100%">
<mx:Button label="Identify" click="drawToolbar.activate
(Draw.MAPPOINT)"/>
<esri:Map id="map" width="100%" height="100%">
<esri:ArcGISDynamicMapServiceLayer
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Special
ty/ESRI_StatesCitiesRivers_USA/MapServer" />
<esri:GraphicsLayer id="myGraphicsLayer"/>
</esri:Map>
</mx:Panel>
</mx:Application>
原文地址:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=35661&extra=page%3D4%26amp%3Borderby%3Ddateline