加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

arcgis api for flex 开发入门(五)查询

发布时间:2020-12-15 04:18:31 所属栏目:百科 来源:网络整理
导读:在gis中,针对要素的查询是一个最基本的操作,也是最常用的操作之一。 下面我们介绍如何使用arcgis api for flex 来查询我们需要的东西。 要在arcgis api for flex中进行查询操作,首先需要定义一个查询任务面板。 使用esri ueryTask标签就可以了。 ??esri u

在gis中,针对要素的查询是一个最基本的操作,也是最常用的操作之一。
下面我们介绍如何使用arcgis api for flex 来查询我们需要的东西。
要在arcgis api for flex中进行查询操作,首先需要定义一个查询任务面板。
使用<esri

ueryTask>标签就可以了。
??<esri

ueryTask id="queryTask"?
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demogra
phics/ESRI_Census_USA/MapServer/5">
? ?? ???<esri

uery id="query"?
? ?? ?? ?? ?text="{qText.text}"
? ?? ?? ?? ?returnGeometry="true"
? ?? ?? ?? ?spatialRelati>
? ?? ?? ?? ?<esri

utFields>
? ?? ?? ?? ?? ? <mx:String>MED_AGE</mx:String>
? ?? ?? ?? ?? ? <mx:String>POP2007</mx:String>
? ?? ?? ?? ?</esri

utFields>
? ?? ???</esri

uery>
? ? </esri

ueryTask
id 唯一标识这个查询任务,url告诉查询面板去哪查。
<esri

uery>定义一个查询,text是你需要查询的东西,<esri

utFields>子标
签告诉Query 查询的结果返回哪些字段的内容。
QueryTask 定义好之后,我们还需要在界面上来调用这个QueryTask。因此我们定
义一个文本输入框和一个查询按钮
<mx

anel title="Query a layer (search for a state)"?
layout="horizontal" backgroundColor="0xB2BFC6" borderStyle="solid">? ???
? ?<mx:TextInput width="100%" id="qText" enter="doQuery()"?
text="California"/>? ?? ???<mx:Button label="Do Query" click="doQuery
()"/>? ?? ?? ?? ?? ? </mx

anel>
文本输入框 用来输入你想要查询的内容,button 用来执行查询的动作。
那么这个doQuery()怎么实现呢?我们在mxml的标签中已经无法实现,这就需要引
入activescript脚本。我们需要在mxml中使用activescript脚本历来编写代码,
实现我们想要的功能。
关于activescript的语法大家可以参考activescript的相关书籍。
要在mxml文档中插入activescript,需要使用<mx:Script>标签
<mx:Script>
? ?? ???<![CDATA[
? ?? ???]]>
? ? </mx:Script>
activescript 是一种类java 语言,它本身有一个AVM,把activescript编译成
java 的代码,然后再通过JVM转换成字节码执行。
我们下面就开始实现doQuery();
首先,我们要用import 指令引入我们需要的命名空间,和java基本一样
<mx:Script>
? ?? ???<![CDATA[
? ?? ?? ?? ?import com.esri.ags.Graphic;
? ?? ?? ?? ?import com.esri.ags.tasks.FeatureSet;
? ?? ?? ?? ?import com.esri.ags.tasks.Query;
? ?? ?? ?? ?import mx.controls.Alert;
? ?? ?? ?? ?import mx.rpc.AsyncResponder;
? ?? ???]]>
? ? </mx:Script>
然后我们定义doQuery()函数: 注意activescript代码 要放到<mx:Script>标签

private function doQuery() : void
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? queryTask.execute( query,new AsyncResponder( onResult,?
onFault ));
在doQuery()函数中直接调用了queryTask的execute方法,这是一个异步调用。
成功响应onResult函数,失败则响应onFault函数。
查询已经写好了,那么我们怎么得到查询的结果呢?得到结果肖恩么表现呢?
这就需要我们在onResult函数中做一些事情了。
首先,定义onResult函数
function onResult( featureSet : FeatureSet,token : Object = null ) :?
void
? ?? ?? ?? ?? ? {? ?
? ?? ?? ?? ?? ?? ???var displayFieldName : String =?
featureSet.displayFieldName;
? ?? ?? ?? ?? ?? ???for each ( var myGraphic : Graphic in?
featureSet.features )
? ?? ?? ?? ?? ?? ???{
? ?? ?? ?? ?? ?? ?? ?? ?// ToolTip
? ?? ?? ?? ?? ?? ?? ?? ?myGraphic.toolTip = "The 2007 population of "?
? ?? ?? ?? ?? ?? ?? ?? ?? ? + myGraphic.attributes[displayFieldName] +?
" was "?
? ?? ?? ?? ?? ?? ?? ?? ?? ? + myNumberFormatter.format
(myGraphic.attributes.POP2007)
? ?? ?? ?? ?? ?? ?? ?? ?? ? + "nMedian Age: " +?
myGraphic.attributes.MED_AGE + ".";? ?? ??
? ?? ?? ?? ?? ?? ?? ?? ?// show on map
? ?? ?? ?? ?? ?? ?? ?? ?myGraphicsLayer.add( myGraphic );?
? ?? ?? ?? ?? ?? ???}
? ?? ?? ?? ?? ? }
查询结果返回一个 FeatureSet,我们现在遍历这个 FeatureSet,然后把每个
feature 绘制到GraphicLayer上。
如果查询失败了怎么办呢,我们是不是要弹个东西出来告诉用户呢?
这就需要我们在onFault函数中做一些工作
function onFault( info : Object,token : Object = null ) : void
? ?? ?? ?? ?? ? {
? ?? ?? ?? ?? ?? ???Alert.show( info.toString() );
? ?? ?? ?? ?? ? }? ?? ?? ?? ?? ?? ?? ?? ??
? ?? ?? ?? ?}
我们弹个对话框出来告诉用户,查找失败啦!

完整代码:

<?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"
    pageTitle="Query Task"
    >    
    <mx:Script>
        <![CDATA[
            import com.esri.ags.Graphic;
            import com.esri.ags.tasks.FeatureSet;
            import com.esri.ags.tasks.Query;
            import mx.controls.Alert;
            import mx.rpc.AsyncResponder;
                        
            private function doQuery() : void
            {
                queryTask.execute( query,onFault ));
                function onResult( featureSet : FeatureSet,token : 
Object = null ) : void
                {   
                    var displayFieldName : String = 
featureSet.displayFieldName;
                    for each ( var myGraphic : Graphic in 
featureSet.features )
                    {
                        // ToolTip
                        myGraphic.toolTip = "The 2007 population of " 
                            + myGraphic.attributes[displayFieldName] + 
" was " 
                            + myNumberFormatter.format
(myGraphic.attributes.POP2007)
                            + "nMedian Age: " + 
myGraphic.attributes.MED_AGE + ".";       
                        // show on map
                        myGraphicsLayer.add( myGraphic ); 
                    }
                }
                function onFault( info : Object,token : Object = null 
) : void
                {
                    Alert.show( info.toString() );
                }                         
            }
        ]]>
    </mx:Script>
    <mx:NumberFormatter id="myNumberFormatter" 
useThousandsSeparator="true"/>
    <!-- Layer with US States -->
    <esri:QueryTask id="queryTask" 
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demogra
phics/ESRI_Census_USA/MapServer/5">
        <esri:Query id="query" 
            text="{qText.text}"
            returnGeometry="true"
            spatialRelationship="esriSpatialRelEnvelopeIntersects">
            <esri:outFields>
                <mx:String>MED_AGE</mx:String>
                <mx:String>POP2007</mx:String>
            </esri:outFields>
        </esri:Query>
    </esri:QueryTask>
    <mx:Panel title="Query a layer (search for a state)" 
layout="horizontal" backgroundColor="0xB2BFC6" borderStyle="solid">
        <mx:TextInput width="100%" id="qText" enter="doQuery()" 
text="California"/>
        <mx:Button label="Do Query" click="doQuery()"/>            
    </mx:Panel>
    <esri:Map>
        <esri:extent>
            <esri:Extent xmin="-170" ymin="15" xmax="-65" ymax="75"/>
        </esri:extent>
        <esri:ArcGISTiledMapServiceLayer
            
url="http://server.arcgisonline.com/ArcGIS/rest/services/NPS_Physical_W
orld_2D/MapServer" />
        <esri:GraphicsLayer id="myGraphicsLayer"/>
    </esri:Map>
</mx:Application>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读