Select to view content in your preferred language

problem with map.graphics.add(graphic);

6984
14
04-29-2014 11:20 AM
GyeyoungChoi
Deactivated User
Hi all,

function getLocRes(results) {
 //map.graphics.clear();
 var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([80, 0, 0]), 2), new dojo.Color([80, 0, 0, 0.5]));
 var combinedExtent = new esri.geometry.Extent();
 console.log("er",results.features.length);
 for (var i = 0;i < results.features.length; i++) {console.log("i=",i);
  var polyGraphic = new esri.Graphic(results.features, symbol);
  console.log("i=",polyGraphic);
  polyGraphic.setSymbol(symbol);
  //map.graphics.add(graphic);
 }
 combinedExtent = esri.graphicsExtent(results.features);
 map.setExtent(combinedExtent, true);
}


this function is working sort of  but when I enable either
map.graphics.clear();
or
map.graphics.add(graphic);
it stops where the code is
console.log("i=",i);
this shows
only 0 when map.graphics.add(graphic); is on
if not it doesn't add graphic but shows console.log("i=",i); result until the end

Am I missing something here that map.graphics.add(graphic); shouldn't work?

dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("esri.map");
dojo.require("esri.dijit.Popup");
dojo.require("esri.dijit.Legend");
dojo.require("esri.layers.agsdynamic");

dojo.require("esri.layers.ArcGISTiledMapServiceLayer");
dojo.require("dijit.dijit");
dojo.require("esri.tasks.find");//search     

dojo.require("esri.arcgis.utils");
dojo.require("esri.dijit.Print");
dojo.require("dojox.grid.DataGrid");

dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.form.Button");
dojo.require("esri.tasks.query");
dojo.require("esri.dijit.Scalebar");
dojo.require("esri.graphic");
dojo.require("esri.symbol");
0 Kudos
14 Replies
JeffPace
MVP Alum
my guess is that you are trying to do the query before the map finishes loading.  Try moving your query.execute into a map.on layers-add-result event

that is why the alert makes it work.  It is basically waiting, giving the map enough time to load
0 Kudos
GyeyoungChoi
Deactivated User
my guess is that you are trying to do the query before the map finishes loading.  Try moving your query.execute into a map.on layers-add-result event

that is why the alert makes it work.  It is basically waiting, giving the map enough time to load


Thanks Jeff! on it
0 Kudos
GyeyoungChoi
Deactivated User
tried
map.on("layers-add-result", queryTask.execute(query, getLocRes, function(error){console.log("error: " + error.message);}));


when I use this code below:
function getLocRes(results) {
 map.graphics.clear();
 var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([80, 0, 0]), 2), new dojo.Color([80, 0, 0, 0.5]));
 var combinedExtent = new esri.geometry.Extent();
 for (var i = 0;i < results.features.length; i++){
  var polyGraphic = results.features;
  polyGraphic.setSymbol(symbol);
  map.graphics.add(polyGraphic);
 }
 combinedExtent = esri.graphicsExtent(results.features);
 map.setExtent(combinedExtent, true);
}

get
error: map.graphics is null
which is same
and tried this but
var polyGraphic = new esri.Graphic(results.features, symbol);
map.graphics.add(polyGraphic);

same
0 Kudos
TracySchloss
Honored Contributor
I think you should be waiting for the queryTask to complete, not the map add layers event.  All my examples I have handy are in AMD style, but you should be able to find some examples that are something like
dojo.connect (queryTask, "onComplete", getLocRes);


Then in the line to execute the query, you won't include the results handler, you'll just run queryTask.execute(query);
0 Kudos
GyeyoungChoi
Deactivated User
I think you should be waiting for the queryTask to complete, not the map add layers event.  All my examples I have handy are in AMD style, but you should be able to find some examples that are something like
dojo.connect (queryTask, "onComplete", getLocRes);


Then in the line to execute the query, you won't include the results handler, you'll just run queryTask.execute(query);


Thank you Tracy! I'll post the result!
0 Kudos