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控件 
<esri raw id="drawToolbar" map="{map}" 
graphicsLayer="{myGraphicsLayer}" drawEnd="drawEndHandler(event)">
记得添加上drawEnd消息的响应函数drawEndHandler(event),这个事件会在draw
之后响应。
用as3脚本实现drawEndHandler和identifyCompleteHandler函数
private function drawEndHandler(event rawEvent):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);
               }

           }

<?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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值