Skip To Content

Query your data

In this topic

Version 3.7

Steps to query data

In this tutorial, you will learn how to work with a query task to query data from an ArcGIS service. There is typically four steps involved in the query process:

  1. Set the Map and GraphicsLayer.
  2. Set the QueryTask using a query filter.
  3. Execute the QueryTask with user inputs.
  4. Display the query results.

The example used in this tutorial is a query task where you type in the name of a US state. Based on this input, all states that match the name are displayed on the map.

Set the Map and GraphicsLayer

The Map and its layers are used to provide a context to displaying the query results. The GraphicsLayer is used to symbolize and display the geometries of the query results. The following code snippet shows a map with an ArcGIS Server tiled map service layer and a graphics layer added to the map.


The following steps assume you have completed the steps necessary to create a new project and add the Flex API library as discussed in the Getting started topic.

  1. In Flash Builder, create a new project and make certain to add the Flex API library and reference the esri namespace.
  2. In the Editor View, add the MXML below including reference to the layout tag.
    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="" 
       <s:VerticalLayout gap="10"
  3. Next, add the map, a tiled layer, and lastly a GraphicsLayer for the query results. The next section will discuss QueryTask which is used in the graphicProvider.
    <esri:Map id="myMap">
          <esri:Extent xmin="-14298000" ymin="2748000" xmax="-6815000" ymax="7117000">
             <esri:SpatialReference wkid="102100"/>
       <esri:GraphicsLayer id="myGraphicsLayer" 


    It is not always necessary to work with a map when performing a query. Another example could include populating a table with the query results. See this sample for additional help on doing this.

Set the QueryTask

The QueryTask is just one of a number of tasks provided by the ArcGIS API for Flex. Regardless of which task you are working with, each has a unique type of parameter that is used to define it. For example, the QueryTask works with a Query object as its parameter. The diagram below depicts this as it illustrates the task working with the defined parameters creates task results.

Task operational flow in Flex API

In order to access the QueryTask, you first need to set it to a URL of the layer that the query will work against. The URL includes the layer ID. In the following code snippet, the layer ID is 0.

  • In Flash Builder's Editor view, add the following code snippet in the <fx:Declarations> section.
    <!-- Layer with US States -->
    <esri:QueryTask id="queryTask"
       <esri:Query id="query"
    <esri:Map id="myMap">

    The query defines the criteria that will be used for executing the query. Set returnGeometry="true" if you wish to display the results of the query. In addition, two fields are specified in the outFields . These two fields could then be displayed in the output query results.


    The fewer fields included, the faster the response. Also, use the actual field names rather than aliases, although aliases can be used in the output task results.

Executing the query

The QueryTask, Query parameter, and resulting FeatureSet work together to query a layer in a map and display the results. The resulting FeatureSet can contain both features and attribute information. The diagram below depicts the conceptual flow of this.

QueryTask conceptual flow with resulting FeatureSet in Flex API

  • In Flash Builder's Editor view, add the following MXML snippet following the <fx:Declarations> section.
    <s:Panel height="60" 
       title="Query a layer (search for a state)">
       <s:TextInput id="qText"
       <s:Button click="doQuery()" label="Do Query"/>
    <esri:Map id="myMap">

    The TextInput allows input of a state name. Once this is entered, the doQuery() function is called. This then executes the callback function onResult(). One of the arguments to this function is a FeatureSet that contains the results of the query. The FeatureSet's features can be directly assigned to the GraphicProvider of the graphics layer rendered on the map.

    In case of a failure, the callback function onFault() is called, which, in the following example, shows an alert box with information about the failure.


    For the purpose of this tutorial, the results are assigned to the GraphicProvider in the MXML code, graphicProvider="{queryTask.executeLastResult.features}".

  • Add the ActionScript code making certain to place it in a fx:Script tag.
          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
        {, "Query Problem");
       <!-- Layer with US States -->
  • Save and run the application.
  • Type in a state name, e.g. California.

Query results on a map using Flex API

You should now have a web application displayed similarly to the screen capture above. Conceptually, this is how the code breaks down:

  • The queryTask is based off of a layer within a map service. In this case, a 'states' layer of the US.
  • The query parameter is set to whatever text is input to the textbox.
  • The onResult() function is fired after successful completion of the queryTask which, in turn, passes in the resulting FeatureSet.
  • The FeatureSet features, ie. graphics, are bound to graphicProvider and subsequently displayed.