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.

  • Is this a one-time load of all graphics? – Aamir Suleman Feb 4 '15 at 20:56
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.