Hide Table of Contents
View Query data without a map sample in sandbox
Query data without a map

Description

This sample demonstrates that you can query data from a map service without displaying the service. Most map services contain datasets with attribute information that can be queried and displayed in a simple list or table. This sample queries USA Census data for a state name that you supply, then displays a list of attribute information about the state.

The code creates a QueryTask along with a Query that is used as input to the task. Notice that the constructor for the QueryTask requires the URL of a layer in a map service (http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5). The number 5 at the end of the URL is the index of the states layer in the map table of contents. To find out the URL to your own map service and its layer indexes, use the Services Directory.

Notice that the query.returnGeometry is set to false because the results won't be displayed on a map. The outFields determine which fields in the states layer will be queried.

When you click the Get Details button, the execute function is called. The query task searches for the text in the primary display field of the layer to be queried, which for this layer is the STATE_NAME field. This is why query.text is set to the state name that you entered in the text box. If you want to enter any SQL where clause, you can use query.where instead of query.text.

With all the properties of the query specified, it can be passed as an argument to the QueryTask, along with a function to be called when the query is complete.

queryTask.execute(query,showResults);

The query results are passed to the showResults function as a FeatureSet. The function loops through all the results' attributes and constructs a list from them using HTML. This HTML is then written to the "info" div.

Code

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Query State Info without Map</title>

    <script src="https://js.arcgis.com/3.29/"></script>
    <script>
      require([
        "dojo/dom", "dojo/on",
        "esri/tasks/query", "esri/tasks/QueryTask", "dojo/domReady!"
      ], function (dom, on, Query, QueryTask) {

        var queryTask = new QueryTask("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");

        var query = new Query();
        query.returnGeometry = false;
        query.outFields = [
          "SQMI", "STATE_NAME", "STATE_FIPS", "SUB_REGION", "STATE_ABBR",
          "POP2000", "POP2007", "POP00_SQMI", "POP07_SQMI", "HOUSEHOLDS",
          "MALES", "FEMALES", "WHITE", "BLACK", "AMERI_ES", "ASIAN", "OTHER",
          "HISPANIC", "AGE_UNDER5", "AGE_5_17", "AGE_18_21", "AGE_22_29",
          "AGE_30_39", "AGE_40_49", "AGE_50_64", "AGE_65_UP"
        ];

        on(dom.byId("execute"), "click", execute);

        function execute () {
          query.text = dom.byId("stateName").value;
          queryTask.execute(query, showResults);
        }

        function showResults (results) {
          var resultItems = [];
          var resultCount = results.features.length;
          for (var i = 0; i < resultCount; i++) {
            var featureAttributes = results.features[i].attributes;
            for (var attr in featureAttributes) {
              resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>");
            }
            resultItems.push("<br>");
          }
          dom.byId("info").innerHTML = resultItems.join("");
        }
      });
    </script>
  </head>

  <body>
    US state name :
    <input type="text" id="stateName" value="California">
    <input id="execute" type="button" value="Get Details">
    <br />
    <br />
    <div id="info" style="padding:5px; margin:5px; background-color:#eee;">
    </div>
  </body>
</html>
 
          
Show Modal