On a web application I'm currently working on, some of the data for the GIS Service is bad. For instance, there may be multiple polygons that should be a singular graphic, but instead they are each their own individual graphic. So what I'm doing right now is creating a hash call GraphicHash which is keyed by the name of each graphic in the attribute.
From there I get each graphic and do a union on their extents.
var GraphicHash = {}; //hash for all the graphics
var ctr = 0; //counter for collisions
var zoom_change = false;
// generate the hash keyed on REC_AREA_NAME, if collision, then add a number to the end of it
recLayer.on("graphic-add", function(evt) {
if (zoom_change == false) {
var cur_graphic = toIDCase(evt.graphic.attributes.REC_AREA_NAME);
if (GraphicHash[cur_graphic] == null) {
GraphicHash[cur_graphic] = evt.graphic;
} else if (GraphicHash[cur_graphic].attributes.STREET_ADDRESS != evt.graphic.attributes.STREET_ADDRESS) {
GraphicHash[cur_graphic + ctr];
ctr++;
} else {
var new_extent = GraphicHash[cur_graphic].geometry.getExtent().union(evt.graphic.geometry.getExtent());
GraphicHash[cur_graphic].geometry.getExtent().update(new_extent);
}
}
});
However, after I have created that hash, I would like to go through each graphic, calculate the centroid and then display it on the map. Currently I am doing the following:
for (var graphic in GraphicHash) {
var centroid = GraphicHash[graphic].geometry.getCentroid();
GraphicHash[graphic].setGeometry(centroid);
GraphicHash[graphic].setSymbol(recSymbol);
//graphic_array.push(GraphicHash[graphic]);
recLayer.add(GraphicHash[graphic]);
}
I do this on the "update-end" event. However, in some instances I get errors where getCentroid() is not a function and other times I get problems where a point exists for one of the hashed values, but it seems to have not included other polygons that needed to be hashed, and those polygons that weren't hashed appear on the map as polygons.
-
Is this a one-time load of all graphics?Aamir Suleman– Aamir Suleman2015年02月04日 20:56:02 +00:00Commented Feb 4, 2015 at 20:56
1 Answer 1
The graphic looks like it was already added to the layer, so resetting a geometry and symbol should work.
You will not be able to re-add the same graphic.
Instead you should create a new instance of the graphic in the for loop.
var g = new Graphic(centroid, recSymbol);
recLayer.add(g);