Select to view content in your preferred language

Simple Query Task Help Needed

3297
13
Jump to solution
01-24-2014 10:48 AM
PaulWilner_II
Deactivated User
Hi All:

I really must be missing something here because I have yet to get a query task to work properly. I'm very new to both javascript and web app development and so I'm taking baby steps to build the app that my organization has requested. As a first step all I'd like to do is query my layer and return one point of 64 based upon the query. In this case based on the query, the point with the name "Albany" in the CampusName field should return the symbol specified in the query and be different than the other points.

This seems to be a simple task and I'm following the guide here:

https://developers.arcgis.com/javascript/jshelp/intro_querytask.html
and trying to apply it to my data.

Any insight would be much appreciated.

Below is my code;

<!DOCTYPE html>
<html>
<head>
  <title>Create a Map</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/dojo/dijit/themes/claro/claro.css">
  <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css">
  <style>
    html, body, #mapDiv{
      padding: 0;
      margin: 0;
      height: 100%;
    }
  </style>
 
  <script src="http://js.arcgis.com/3.8/"></script>
  <script>
    var map;
    require(["esri/map", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/tasks/QueryTask", "esri/tasks/query", "esri/symbols/SimpleMarkerSymbol", "esri/InfoTemplate", "dojo/_base/Color", "dojo/domReady!"],
   
    function(Map,ArcGISDynamicMapServiceLayer, QueryTask,Query, SimpleMarkerSymbol, InfoTemplate, Color)
    {
      map = new Map("mapDiv",
      {
        center: [-56.049, 38.485],
        zoom: 3,
        basemap: "streets"
       
       
      });
       var SunySchoolsLayer = new ArcGISDynamicMapServiceLayer("http://w7hp348/arcgis/rest/services/Testservice/FeatureServiceTest/MapServer/");
       map.addLayer(SunySchoolsLayer);
      
       queryTask=new QueryTask("http://w7hp348/arcgis/rest/services/Testservice/FeatureServiceTest/MapServer/0");
      
       query = new Query();
       query.returnGeometry = true;
       query.outFields = ["CampusName"];
      
       infoTemplate = new InfoTemplate ("${CampusName}");
      
       symbol=new simpleMarkerSymbol();
       symbol.setStyle(SimpleMarkerSymbol.STYLE_SQUARE);
       symbol.setSize(10);
       symbol.setColor(new Color([255,255,0,0.5]));
      
     
      
       function executeQueryTask()
       {
         query.where = "CampusName = 'Albany' ";
         queryTask.execute(query,showResults);
       }
      
       function showResults(featureSet)
       {
         for(vari=0, i1=resultFeatures.length; i<i1; i++)
         {
           var graphic=resultFeatures;
           graphic.setSymbol(symbol);
           graphic.setInfoTEmplate(infoTemplate);
           map.graphics.add(graphic);
         }
       }
    });
0 Kudos
13 Replies
PaulWilner_II
Deactivated User
Hi All:

I thought I'd try to revive this thread. I found out that the type f undefined error had something to do with the spatial reference. I've commented out the spatial reference line in the query task  and this seems to solve the problem.

Now my query is returning 0 features, and is undefined, even when I changed the "query.where" statement to 1=1. Why am I returning a featureset array of 0.

Here is my current code, Any help would be much appreciated.

<!DOCTYPE html>
<html>
<head>
  <title>Create a Map</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/dojo/dijit/themes/tundra/tundra.css">
  <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css">
  <style>
    html, body, #mapDiv{
      padding: 0;
      margin: 0;
      height: 100%;
    }
  </style>
  
  <script src="http://js.arcgis.com/3.8/"></script>
  <script>
 var map;
 require(["esri/map","esri/layers/ArcGISDynamicMapServiceLayer","esri/tasks/QueryTask","esri/tasks/query","esri/symbols/SimpleMarkerSymbol","esri/tasks/FeatureSet","dojo/domReady!"],
 function(Map,ArcGISDynamicMapServiceLayer,QueryTask,Query, SimpleMarkerSymbol,FeatureSet)
 {
map = new Map("mapDiv", 
      {
        center: [-56.049, 38.485],
        zoom: 3,
        basemap: "streets"     
      });
      
      var SunySchoolsLayer = new ArcGISDynamicMapServiceLayer("http://w7hp348/arcgis/rest/services/Testservice/XTest/MapServer");
       map.addLayer(SunySchoolsLayer);
       
       
       

       
       
       var queryTask= new QueryTask("http://w7hp348/arcgis/rest/services/Testservice/XTest/MapServer/0");
       var query = new Query();
       
       
       
       query.where ="CampusName= 'Albany'";
       //query.outSpatialReference = map.spatialReference;
       query.returnGeometry = true;
       query.outFields = ["CampusName"];
       queryTask.execute(query,showResults);
       
       //console.log("My query results:", featureSet.features); 
       
       
       
       var symbol = new SimpleMarkerSymbol();
       symbol.setStyle(SimpleMarkerSymbol.STYLE_SQUARE);
       symbol.setSize(50);
       
       var resultFeatures=FeatureSet.features;
       
       console.log("My query results:", FeatureSet.length);
       console.log("My query results:", FeatureSet.features);   //returns an array of your query result's features
       
       function showResults(resultFeatures)
       
       {
                         
         for(var i=0, i1=resultFeatures.features.length; i<i1; i++);
         {
           var graphic=resultFeatures;
           graphic.setSymbol(symbol);
           map.graphics.add(graphic);
                    
         }
         

       }
       
       
    });
  


  </script>
  
</head>
<body class="tundra">
  <div id="mapDiv"></div>;
</body>
</html>
0 Kudos
PaulWilner_II
Deactivated User
With a little more debugging I got this to work; I think the query was executing properly, the problem appeared to be on the display side of things.

Thanks to all that helped, I'm going to consider this thread closed now.
0 Kudos
KenBuja
MVP Esteemed Contributor
Thanks to all that helped, I'm going to consider this thread closed now.      


You should consider clicking the up arrow on the posts that were helpful and click the check mark on the post (even if it's yours) on what you consider to be the best answer (see this page for more detail). This will help others who may be searching on this type of question
0 Kudos
PaulWilner_II
Deactivated User
Excellent, that was helpful; I didn't really take notice of the arrows and check marks; I'll be sure to credit  those who deserve it in the future!
0 Kudos