Arcgis api for flex development (5) Query

Source: Internet
Author: User
Tags cdata

In gis, querying for elements is the most basic operation and one of the most common operations.
The following describes how to use arcgis api for flex to query what we need.
To perform query operations in arcgis api for flex, you must first define a Query Task panel.
Use the <esriueryTask> label.
<EsriueryTask id = "queryTask"
Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demogra
Phics/ESRI_Census_USA/MapServer/5 ">
<Esriuery id = "query"
Text = "{qText. text }"
ReturnGeometry = "true"
SpatialRelati>
<EsriutFields>
<Mx: String> MED_AGE </mx: String>
<Mx: String> POP2007 </mx: String>
</EsriutFields>
</Esriuery>
</EsriueryTask
The id uniquely identifies the query task. The url tells the query panel where to query the task.
<Esriuery> defines a query. text is what you need to query. <esriutFields> sub-mark
Indicates the fields returned by the Query results.
After QueryTask is defined, we also need to call this QueryTask on the interface. Therefore
Define a text input box and a query button
<Mxanel 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
() "/> </Mxanel>
The text input box is used to enter the content you want to query, and the button is used to perform the query action.
How can this doQuery () be implemented? We cannot implement it in the mxml tag, which requires reference
Enter the activescript script. We need to use activescript scripts in mxml to write code from time to time,
Implement the functions we want.
For more information about activescript syntax, see related books of activescript.
To insert activescript into the mxml file, you need to use the <mx: Script> label
<Mx: Script>
<! [CDATA [
]>
</Mx: Script>
Activescript is a type of java language. It has an AVM and compiles activescript
Java code, and then convert it to bytecode through JVM for execution.
We will start to implement doQuery () below ();
First, we need to use the import command to introduce the namespace we need, which is basically the same as that in 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>
Then we define the doQuery () function: note that the activescript code should be placed in the <mx: Script> label.
Medium
Private function doQuery (): void
{
QueryTask.exe cute (query, new AsyncResponder (onResult,
OnFault ));
The execute method of queryTask is directly called in the doQuery () function, which is an asynchronous call.
The onResult function is successfully returned, and the onFault function is returned if the onResult function fails.
The query has been written. How can we get the query result? What is the result of Sean?
This requires us to do something in the onResult function.
First, define the onResult function.
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"
+ MyGraphic. attributes [displayFieldName] +
"Was"
+ MyNumberFormatter. format
(MyGraphic. attributes. POP2007)
+ "\ NMedian Age:" +
MyGraphic. attributes. MED_AGE + ".";
// Show on map
MyGraphicsLayer. add (myGraphic );
}
}
The query result returns a FeatureSet. We will traverse this FeatureSet and then
The feature is drawn to GraphicLayer.
What should we do if the query fails? Do we need to pop up something to tell the user?
This requires us to do some work in the onFault function.
Function onFault (info: Object, token: Object = null): void
{
Alert. show (info. toString ());
}
}
A dialog box is displayed, indicating that the search failed!

Complete code:

Code
<? 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.exe cute (query, new AsyncResponder (onResult,
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"
+ 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>

Original address: http://bbs.esrichina-bj.cn/ESRI/viewthread.php? Tid = 35621 & extra = page % 3D4% 26amp % 3 Borderby % 3 Ddateline

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.