QueryTask类特点:
- 只能查询一个图层,url必须指定图层
- 可以进行空间查询和属性查询 Text 与Where语句,当where语句存在时 text失效
- 可以指定返回字段
- 处理结果为一个FeatureSet对象
步骤:
- 创建MAP和GraphicSlayer
- 创建QueryTask类
- 创建Query类作为参数
- 执行QueryTask的execute方法
- 显示方法结果
<?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.FeatureSet;
import com.esri.ags.events.DrawEvent;
import com.esri.ags.geometry.Geometry;
import mx.rpc.AsyncResponder;
protected function button1_clickHandler(event:MouseEvent):void
{
drawtool.activate(com.esri.ags.tools.DrawTool.POLYGON);//画一个多变形
}
protected function drawtool_drawEndHandler(event:DrawEvent):void
{
var geometry1:Geometry = event.graphic.geometry;//从画图结束的时候的event参数中得到几何图形
query.geometry = geometry1;//空间查询所用的对比几何图形
query.spatialRelationship=Query.SPATIAL_REL_CONTAINS;//所需要查询的控件关系[包含]
QueryTask1.execute(query,new AsyncResponder(onok,onerr));//异步执行Query查询
}
protected function onok(info:FeatureSet,token:Object):void
{
graphic1.graphicProvider = info.features;//因为得到的是Featureset,将features作为数据源提供给graphiclayer
grid1.dataProvider = info.attributes;//将得到的attributes作为Gridview数据源
}
protected function onerr(info:Object,token:Object):void
{
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<esri:DrawTool id="drawtool" drawEnd="drawtool_drawEndHandler(event)" map="{map1}" graphicsLayer="{graphic1}" />
<esri:QueryTask id="QueryTask1" url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0" useAMF="false" showBusyCursor="true" />
<esri:Query id="query" returnGeometry="true" outFields="['CAPITAL','CITY_NAME']" where="CITY_NAME like '%a%'" />
<esri:PictureMarkerSymbol id="pic1" source="user4.png" />
</fx:Declarations>
<esri:Map id="map1">
<esri:ArcGISDynamicMapServiceLayer url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer" />
<esri:GraphicsLayer id="graphic1" symbol="{pic1}" />
</esri:Map>
<s:Button x="63" y="10" label="Button" click="button1_clickHandler(event)"/>
<mx:DataGrid x="49" y="39" id="grid1">
<mx:columns>
<mx:DataGridColumn headerText="首都" dataField="CAPITAL"/>
<mx:DataGridColumn headerText="城市名称" dataField="CITY_NAME"/>
</mx:columns>
</mx:DataGrid>
</s:Application>