QueryTask: graphics not being drawn?

622
2
Jump to solution
08-09-2013 03:41 PM
RobertMartin2
Occasional Contributor II
Hi all,

I am trying to implement a query task in my JS app per this tutorial:

https://developers.arcgis.com/en/javascript/jshelp/intro_querytask.html

After a little finagling everything seems to be working fine -- the query returns the expected feature and the callback gets called. But nothing is showing up on the map. Are there any caveats I should know about? This is how I initialize the query, etc.:

// Initialize query task queryTask = new QueryTask('http://myurl');  // Initialize query query = new Query(); query.returnGeometry = true; query.outFields = ['PIND'];  // Initialize info template infoTemplate = new InfoTemplate('${PIND}', 'PIND: ${PIND}');  // Initialize symbol symbol = new SimpleFillSymbol(  SimpleFillSymbol.STYLE_SOLID,  new SimpleLineSymbol(   SimpleLineSymbol.STYLE_SOLID,   new Color([255.0, 0.0, 0.0])  ),  new Color([255.0, 0.0, 0.0, 0.25]) );



And this is my drawing function:

function drawFeatureSet(featureSet) {  try {   // Clear graphics   map.graphics.clear();    // Loop through features   for (feature in featureSet.features) {    feature.symbol = symbol;    feature.infoTemplate = infoTemplate;    map.graphics.add(feature);   }  }  catch(e) {   console.log(e.message);  } }


If I try logging the contents of the graphics layer after adding all I see is ["0"]. Strange...

Thanks for your help!
0 Kudos
1 Solution

Accepted Solutions
derekswingley1
Frequent Contributor
featureSet.features is an array, not an object so doing for ... in isn't applicable. Instead use something like the showResults function from the page you linked to:

function showResults(featureSet) {     //remove all graphics on the maps graphics layer     map.graphics.clear();      //Performance enhancer - assign featureSet array to a single variable.     var resultFeatures = featureSet.features;      //Loop through each feature returned     for (var i=0, il=resultFeatures.length; i<il; i++) {       //Get the current feature from the featureSet.       //Feature is a graphic       var graphic = resultFeatures;       graphic.setSymbol(symbol);        //Set the infoTemplate.       graphic.setInfoTemplate(infoTemplate);        //Add graphic to the map graphics layer.       map.graphics.add(graphic);     }   }


Also note that setSymbol() method is used rather than setting the symbol property directly.

View solution in original post

0 Kudos
2 Replies
derekswingley1
Frequent Contributor
featureSet.features is an array, not an object so doing for ... in isn't applicable. Instead use something like the showResults function from the page you linked to:

function showResults(featureSet) {     //remove all graphics on the maps graphics layer     map.graphics.clear();      //Performance enhancer - assign featureSet array to a single variable.     var resultFeatures = featureSet.features;      //Loop through each feature returned     for (var i=0, il=resultFeatures.length; i<il; i++) {       //Get the current feature from the featureSet.       //Feature is a graphic       var graphic = resultFeatures;       graphic.setSymbol(symbol);        //Set the infoTemplate.       graphic.setInfoTemplate(infoTemplate);        //Add graphic to the map graphics layer.       map.graphics.add(graphic);     }   }


Also note that setSymbol() method is used rather than setting the symbol property directly.
0 Kudos
RobertMartin2
Occasional Contributor II
That must be it! Thanks Derek. I was thinking the for...in would work along the lines of the Python equivalent. I'll try this out.
0 Kudos