Graphics _shape are null after using graphicsUtils.graphicsExtent

4425
2
Jump to solution
01-22-2015 07:47 AM
YohanBienvenue
Occasional Contributor II

Hi,

I have a problem when using graphicsUtils.graphicsExtent with an Array that contains a reference to some Graphics of my FeatureLayer.

Here's the code that puts the reference to the graphics that I want in an array, creates an extent for them and then zooms the map with this extent:

                 groupGraphics: null,

                /**
                 * Zoom the map for the active contacts
                 *
                 * @param {Object} parameters
                 *
                 * parameters:
                 * {String} layerId The name of the GraphicsLayer
                 * {String} attributeName
                 */
                zoomFeatureGroup: function (parameters) {
                    util.log('MapArcGIS::zoomFeatures');
                   
                    var graphicsLayer = this.map.getLayer(parameters.layerId);
                    this.groupGraphics = [];
                   
                    array.forEach(graphicsLayer.graphics, lang.hitch(this, function(graphic){
                        if (graphic.attributes[parameters.attributeName] !== this.RendererValue.DEFAULT){
                            this.groupGraphics.push(graphic);
                        }
                    }));

                    var extent = graphicsUtils.graphicsExtent(this.groupGraphics);
                    this.map.setExtent(extent, true);
                }

This by itself works, but afterwards when trying to use something like graphic.getDojoShape().moveToBack() in another function, I noticed that all the graphics in the GraphicsLayer have null _shape property (not just those of my subgroup). This creates the following error in Chrome:

Uncaught TypeError: Cannot read property 'moveToBack' of null MapArcGIS.js:776

This only seems to happen if I do both things: 1) create a sub-array of Graphics and 2) use graphicsUtils.graphicsExtent on them. If I do only one or the other, _shape is fine.

I'm not sure if I'm doing something wrong. Any idea?

Edit: After some further debugging I think that _shape is null whenever the Graphic is not visible on the map. So if I zoom and some Graphics are not visible, if I try to iterate on all Graphics and try to call getDojoShape() on them, it will generate an error for those not visible.

I guess I can protect myself with something like this to prevent JavaScript errors and making the whole site unstable:

if ( graphic.getDojoShape() ){

   graphic.getDojoShape().moveToBack()

}

Thanks,

Yohan

0 Kudos
1 Solution

Accepted Solutions
JohnGrayson
Esri Regular Contributor

Yohan,

    what I've seen in the past is the _shape property will be null if they're not in the current map extent.  if you'd like to call 'moveToBack()', you should first make sure the map has finished the 'setExtent()' call.  Not sure if this will work in your use case, but this is how I would typically handle this situation:

this.map.setExtent(extent, true).then(lang.hitch(this, function(){   
    array.forEach(this.groupGraphics, lang.hitch(this, function(graphic){
          graphic.getDojoShape().moveToBack();
    }));
}));

View solution in original post

0 Kudos
2 Replies
JohnGrayson
Esri Regular Contributor

Yohan,

    what I've seen in the past is the _shape property will be null if they're not in the current map extent.  if you'd like to call 'moveToBack()', you should first make sure the map has finished the 'setExtent()' call.  Not sure if this will work in your use case, but this is how I would typically handle this situation:

this.map.setExtent(extent, true).then(lang.hitch(this, function(){   
    array.forEach(this.groupGraphics, lang.hitch(this, function(graphic){
          graphic.getDojoShape().moveToBack();
    }));
}));
0 Kudos
YohanBienvenue
Occasional Contributor II

Oh thanks John, I had not seen your reply and came to a similar conclusion while debugging some more (see my edit in my question). Will mark as answered.

0 Kudos