Zoom to union of multiple layers?

2171
2
09-17-2014 05:21 AM
KarenEllett
Occasional Contributor

I know how to zoom to the full extent of one layer.  However, I have three graphic layers that are created at the same time, and all three are displayed at the same time.  I would like to zoom to the full extent of all three.  Can anyone give me some help on how to do that?

Thanks!

0 Kudos
2 Replies
AsserSwelam1
Occasional Contributor

Hi Karen,

You can do that by looping over the three graphic layers you have and create a extent full all of them and zoom to that extent.

That will be done by comparing each XMin and Ymin of each of this graphic layers and the widest one of them (depend on the spatial reference you are using) you will use it as the XMin and YMin for a new created Envelope. Then do the same for the XMax and YMax.

Then zoom to that Envelope extent.

Same as this image

ZoomTo.png

Lets say that your map have a base tiled layer and this 3 GraphicsLayers

you can achieve what you want by this code below

Envelope ZoomToExtent = new Envelope();

            for (int x = 1; x < 3; x++)

            {

                GraphicsLayer currentGLayer = MyMap.Layers as GraphicsLayer;

                for (int y = 1; y < 3; y++)

                {

                    GraphicsLayer comparedGLayer = MyMap.Layers as GraphicsLayer;

                    if (currentGLayer.FullExtent.XMax > comparedGLayer.FullExtent.XMax)

                        ZoomToExtent.Extent.XMax = currentGLayer.FullExtent.XMax;

                    if (currentGLayer.FullExtent.YMax > comparedGLayer.FullExtent.YMax)

                        ZoomToExtent.Extent.YMax = currentGLayer.FullExtent.YMax;

                    if (currentGLayer.FullExtent.XMin < comparedGLayer.FullExtent.XMin)

                        ZoomToExtent.Extent.XMin = currentGLayer.FullExtent.XMin;

                    if (currentGLayer.FullExtent.YMin < comparedGLayer.FullExtent.YMin)

                        ZoomToExtent.Extent.YMin = currentGLayer.FullExtent.YMin;

                } 

            }

            MyMap.ZoomTo(ZoomToExtent);

Hope that helps

Thanks,

Asser

0 Kudos
KarenEllett
Occasional Contributor

Thanks!  I actually tried something like that and was still having some issues, so what I ended up doing was looking at the full extent of each layer and putting each XMin, Xmax, etc into an array.  Then I created an envelope using XMaxArray.Max, etc.  Works!

Thanks for the help!

0 Kudos