Get Extent from a GraphicsLayer

4282
4
Jump to solution
06-19-2012 02:29 AM
JesusSanchez
New Contributor
Hi everybody!

    I'm trying to get the extent (as a Geometry object) from a graphics layer in which i have added several polylines,  and set the map's extent with this polygon, this way:

    Polygon poligonoRutas = graphicsLayerLines.getExtent();
    if(poligonoRutas != null){
map.setExtent(poligonoRutas);
    }

    I've checked that the polygon returned has vertices, i get no error but it seems that the extent is not working. I've also tried to get the fullExtent form the graphics layer but the envelope object returned is null (i don't know the reason, because the graphics layer has several polylines). Do you know what i'm doing wrong?

Thank's in advance!
0 Kudos
1 Solution

Accepted Solutions
deleted-user-ATjHIWsdQYmT
Occasional Contributor III
That's because you are setting the Envelope to the extent of the last point in your iteration.  Try this:
Envelope env = new Envelope(); Envelope NewEnv = new Envelope(); for (int i:graphicsLayerLines.getGraphicIDs()){ Polyline p = (Polyline)graphicsLayerLines.getGraphic(i).getGeometry();     p.queryEnvelope(env);     NewEnv.merge(env); } for (int i:graphicsLayerPoints.getGraphicIDs()){   Point p = (Point)graphicsLayerPoints.getGraphic(i).getGeometry();   p.queryEnvelope(env);   NewEnv.merge(env); }  map.setExtent(NewEnv);

View solution in original post

0 Kudos
4 Replies
deleted-user-ATjHIWsdQYmT
Occasional Contributor III
What I've done to accomplish this is build an envelope and merge the extent of each graphic into the envelope.  Here's how:
Envelope env = new Envelope();
Envelope NewEnv = new Envelope();
    for (int i:graphicsLayer.getGraphicIDs()){
 Polygon p = (Polygon) graphicsLayer.getGraphic(i).getGeometry();
 p.queryEnvelope(env);
 NewEnv.merge(env);
   } 

 MapView.setExtent(NewEnv);
0 Kudos
JesusSanchez
New Contributor
Thank's andrewb! It was a great solution. I made a cast to Polyline instead Polygon for each graphic, but it's working great. I have been trying one more thing, now i have two graphics layer, one for lines and the other one for points. I've tried this:

    Envelope env = new Envelope();
    Envelope NewEnv = new Envelope();
    for (int i:graphicsLayerLines.getGraphicIDs()){
        Polyline p = (Polyline)graphicsLayerLines.getGraphic(i).getGeometry();
p.queryEnvelope(env);
    }
    for (int i:graphicsLayerPoints.getGraphicIDs()){
        Point p = (Point)graphicsLayerPoints.getGraphic(i).getGeometry();
        p.queryEnvelope(env);
    }
    NewEnv.merge(env);
    map.setExtent(NewEnv);

This is making an extent to the last point, but i want an extent to an envelope which contains all the lines and all the points. Any idea how to do it?

Thanks!
0 Kudos
deleted-user-ATjHIWsdQYmT
Occasional Contributor III
That's because you are setting the Envelope to the extent of the last point in your iteration.  Try this:
Envelope env = new Envelope(); Envelope NewEnv = new Envelope(); for (int i:graphicsLayerLines.getGraphicIDs()){ Polyline p = (Polyline)graphicsLayerLines.getGraphic(i).getGeometry();     p.queryEnvelope(env);     NewEnv.merge(env); } for (int i:graphicsLayerPoints.getGraphicIDs()){   Point p = (Point)graphicsLayerPoints.getGraphic(i).getGeometry();   p.queryEnvelope(env);   NewEnv.merge(env); }  map.setExtent(NewEnv);
0 Kudos
JesusSanchez
New Contributor
That's right, andrewb. I made an stupid mistake, thanks for your help.
0 Kudos