arcgis for flex api version3.7 教程:3.如何使用QueryTask查询地图服务

来源:互联网 发布:php 数组最大值 编辑:程序博客网 时间:2024/04/30 07:24
这篇介绍如何通过query task来从ArcGIS service中查询数据,分下面四步:
1  设置Map和GraphicsLayer.
2  使用query filter来设置QueryTask
3  输入查询参数执行QueryTask
4  显示查询结果


下面的例子是通过query task来根据输入的美国城市名来查询出所有相关的城市在地图上显示。
首先是设置地图和图层,地图和图层为查询结果展示提供了显示环境。GraphicsLayer用来展示查询


结果的几何图形并将其符号化。下面的代码示例就是使用了ArcGIS Server的瓦片服务和一个


graphics layer来实现的。
(1)在Flash builder中,新建工程,加入api库,引用esri的命名空间
(2)下面代码是引用命名空间的MXML标记语言代码,及布局基本属性的设置
<?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"   xmlns:esri="http://www.esri.com/2008/ags"><s:layout>   <s:VerticalLayout gap="10"   horizontalAlign="center"   paddingBottom="20"   paddingLeft="25"   paddingRight="25"   paddingTop="20"/></s:layout></s:Application>




(3)接下来,添加地图,瓦片服务、添加一个Graphicslayer来显示查询结果。
...paddingTop="20"/></s:layout><esri:Map id="myMap">   <esri:extent>      <esri:Extent xmin="-14298000" ymin="2748000" xmax="-6815000" ymax="7117000">         <esri:SpatialReference wkid="102100"/>      </esri:Extent>   </esri:extent>      <esri:ArcGISTiledMapServiceLayer       url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Physical_Map/MapServer"/>   <esri:GraphicsLayer id="myGraphicsLayer"       graphicProvider="{queryTask.executeLastResult.features}"/></esri:Map></s:Application>


提示:并不是所有的查询结果都是在地图上显示,有的查询结果可以是表单数据。


设置QueryTask,QueryTask只是ArcGIS API for Flex中一些查询工具对象中的一种。每一种查询工


具都会有特定类型的参数需要定义。例如,QueryTask是通过设置Query object作为参数。
在使用QueryTask之前,你需要先对其设置需要进行查询操作的数据源(地图服务)的URL地址,URL


地址中具体到图层ID,在下面的代码示例中,图层ID为0。
提示:像QueryTask这种不可视的对象,在使用MXML标签时,需要写在<fx:Declarations>标签中。
<fx:Declarations><!-- Layer with US States --><esri:QueryTask id="queryTask"   showBusyCursor="true"   url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"   useAMF="false"/>   <esri:Query id="query"      outSpatialReference="{myMap.spatialReference}"      returnGeometry="true"      text="{qText.text}">            <esri:outFields>         <fx:String>MED_AGE</fx:String>         <fx:String>POP2007</fx:String>      </esri:outFields>   </esri:Query></fx:Declarations><esri:Map id="myMap">   <esri:extent>...


在上面的代码中,设置returnGeometry="true",是表示将查询到的Geometry也作为结果数据返回。代码中还设置了两个输出字段(outFields),这两个字段将会输出到查询结果 中。
提示:输出字段越少,查询效率越高。并且使用实际字段名比使用别名效率更高。


执行查询过程,QueryTask、Query parameter(查询参数对象)和作为结果数据的FeatureSet对象,共同完成对一个地图图层 的查询及结果 显示。FeatureSet同时包含features和attribute信息。
添加以下代码:
...   </esri:Query></fx:Declarations><s:Panel height="60"    backgroundColor="0xB2BFC6"   title="Query a layer (search for a state)">   <s:layout>      <s:HorizontalLayout/>   </s:layout>      <s:TextInput id="qText"      width="100%"      enter="doQuery()"      text="California"/>   <s:Button click="doQuery()" label="Do Query"/></s:Panel><esri:Map id="myMap">   <esri:extent>   ...


代码中的输入框(TextInput)允许你输入一个城市名。输入完按回车键,响应doQuery()函数。进而执行回调函数onResult(),该函数中的第一个参数就是返回的查询结果FeatureSet。FeatureSet可以直接作为Graphicslayer的数据提供GraphicProvider对象的参数。如果查询没有顺利完成,将会回调onFault()函数,显示错误信息。
提示:上述代码中,已经将查询结果数据作为数据源绑定到要显示的GraphicLayer上面了,既:graphicProvider="{queryTask.executeLastResult.features}",一旦有结果返回就会直接显示到地图上,无需在通过as代码在onResult()函数中设置。
as代码要添加在fx:script标签中,代码如下
paddingTop="20"/></s:layout><fx:Script>   <![CDATA[      import com.esri.ags.FeatureSet;      import mx.controls.Alert;      import mx.rpc.AsyncResponder;   private function doQuery():void   {    queryTask.execute(query, new AsyncResponder(onResult, onFault));       function onResult(featureSet:FeatureSet, token:Object = null):void    {      // No code needed in this simple sample, since the      // graphics layer is bound to the query result using      // graphicProvider="{queryTask.executeLastResult.features}"    }    function onFault(info:Object, token:Object = null):void    {     Alert.show(info.toString(), "Query Problem");    }   }   ]]></fx:Script><fx:Declarations>   <!-- Layer with US States -->


运行程序查看结果 

例如输入一个城市名称:California.





1 0
原创粉丝点击