Graphics Layer getting bounds or center of all graphics

1673
1
Jump to solution
04-09-2021 05:15 PM
by Anonymous User
Not applicable

When using a graphics layer how can you get the center of all graphics? I looked into fullExtent, but the values are always 0 and not what you would expect them to be. Is there some kind of tool I'm not finding in the documentation that I can use?

1 Solution

Accepted Solutions
EugeneStaten
New Contributor III

Hi Mitchell112,

The fullExtent property of GraphicsLayer represents the entire earth, with the intersection of the equator and the prime meridian as the center(0,0).

If you are getting the extents of a graphic layer to submit to the view for zooming, the view does this for you from the layre's graphics:

           view.goTo(graphicsLayer.graphics.toArray());

However, if you want these values for other purposes, a simple way is this. (There is, probably, a utility to get extents of an array of geometries, but this works just fine):

        //First, and obviously you want your layer to have all of its graphics, an empty layer wont work.

       graphicsLayer.graphics.push(...myArrayOfGraphics);//the ellipse (...) iterates the push method.

       // Start with the first graphic and iterate the layer's graphics to build extent

       let extent = graphicsLayer.graphics.getItemAt(0).geometry.extent;
      graphicsLayer.graphics.forEach((graphic)=>{
          let gextent = graphic.geometry.extent;
          if(!gextent.equals(extent)){//this should only skip the first one
             extent = extent.union(gextent);
         }
      });
      let center = exent.center;// The center of the layer's extent.

      //extent, above will be the entire extent of the layer's graphics.

View solution in original post

1 Reply
EugeneStaten
New Contributor III

Hi Mitchell112,

The fullExtent property of GraphicsLayer represents the entire earth, with the intersection of the equator and the prime meridian as the center(0,0).

If you are getting the extents of a graphic layer to submit to the view for zooming, the view does this for you from the layre's graphics:

           view.goTo(graphicsLayer.graphics.toArray());

However, if you want these values for other purposes, a simple way is this. (There is, probably, a utility to get extents of an array of geometries, but this works just fine):

        //First, and obviously you want your layer to have all of its graphics, an empty layer wont work.

       graphicsLayer.graphics.push(...myArrayOfGraphics);//the ellipse (...) iterates the push method.

       // Start with the first graphic and iterate the layer's graphics to build extent

       let extent = graphicsLayer.graphics.getItemAt(0).geometry.extent;
      graphicsLayer.graphics.forEach((graphic)=>{
          let gextent = graphic.geometry.extent;
          if(!gextent.equals(extent)){//this should only skip the first one
             extent = extent.union(gextent);
         }
      });
      let center = exent.center;// The center of the layer's extent.

      //extent, above will be the entire extent of the layer's graphics.