<?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)"
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的数据源。