1

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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Feb 4, 2015 at 19:27
1
  • Is this a one-time load of all graphics? Commented Feb 4, 2015 at 20:56

1 Answer 1

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);
answered Feb 5, 2015 at 15:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.