Select to view content in your preferred language

Update the size of the extent

3310
3
10-28-2014 06:54 PM
LukeCatania
Occasional Contributor

I add layers to my map and the mapview takes on the extent of the first layer added.  I cannot pan to any other layers added due to being constrained to the extent of the first layer added.  How do I expand my extent?  Do I need to do this programatically by taking the min and max of all extents and then setting the new mapview extent.  I did try changing the extent after it was set by the first layer using setExtent on mapview, but it did nothing.

0 Kudos
3 Replies
ShellyGill1
Esri Contributor

Hi Luke,

The setExtent method will set the current extent of the map - try looking at MapView.setMaxExtent method instead, to set the maximum boundary extent of the map.

Alternatively, if you want to be able to show any layer at all, you could try adding a basemap layer that has global coverage. The MapView gets its maximum possible extent and also the possible zoom levels from the basemap layer (first added to the map), as you found, so a global basemap provides a base for anything you could add. Note it also sets the spatial reference of the map, so be careful if working with data in different spatial references. The Hello World Maps sample shows you how to do this in layout XML, or a number of samples demonstrate doing this programmatically (e.g. SwitchMaps sample).

0 Kudos
LukeCatania
Occasional Contributor

The setMaxExtent did what I needed.  But, unless my logic is wrong, it seems to make the extent bigger than I want it to be. Is there a large buffer around layers as opposed to be the extent being not to far off from the corners of the layer. I basically want my mapview to zoom to just the size of a layer.  I created a method to basically do what I want, though all my layers are not within the view nor centered .  I still have to pan to center them all as you can see below.  First image is what my code does and second is after I panned.

InitialView.png

PannedView.png

  public Envelope expandExtent(MapView mapView, boolean zoomToMaxResolution) {

        Layer layers[] = mapView.getLayers();

        Envelope currentMaxExtent = mapView.getMaxExtent();

        Point currentMaxExtentUL = currentMaxExtent.getUpperLeft();

        Point currentMaxExtentUR = currentMaxExtent.getUpperRight();

        Point currentMaxExtentLR = currentMaxExtent.getLowerRight();

        Point currentMaxExtentLL = currentMaxExtent.getLowerLeft();

        Envelope layerFullExtent = null;

        Point layerFullExtentUL = null;

        Point layerFullExtentUR = null;

        Point layerFullExtentLR = null;

        Point layerFullExtentLL = null;

        /**

         * Loop through all the current layers to make sure we are at the

         * maximum possible extend that bounds all layers.

         */

        for (int i = 0; i < layers.length; i++) {

            layerFullExtent = mapView.getLayer(i).getFullExtent();

            layerFullExtentUL = layerFullExtent.getUpperLeft();

            layerFullExtentUR = layerFullExtent.getUpperRight();

            layerFullExtentLR = layerFullExtent.getLowerRight();

            layerFullExtentLL = layerFullExtent.getLowerLeft();

            if (layerFullExtentUL.getY() > currentMaxExtentUL.getY())

                currentMaxExtentUL.setY(layerFullExtentUL.getY());

            if (layerFullExtentUL.getX() < currentMaxExtentUL.getX())

                currentMaxExtentUL.setX(layerFullExtentUL.getX());

            if (layerFullExtentUR.getY() > currentMaxExtentUR.getY())

                currentMaxExtentUR.setY(layerFullExtentUR.getY());

            if (layerFullExtentUR.getX() > currentMaxExtentUR.getX())

                currentMaxExtentUR.setX(layerFullExtentUR.getX());

            if (layerFullExtentLR.getY() < currentMaxExtentLR.getY())

                currentMaxExtentLR.setY(layerFullExtentLR.getY());

            if (layerFullExtentLR.getX() > currentMaxExtentLR.getX())

                currentMaxExtentLR.setX(layerFullExtentLR.getX());

            if (layerFullExtentLL.getY() < currentMaxExtentLL.getY())

                currentMaxExtentLL.setY(layerFullExtentLL.getY());

            if (layerFullExtentLL.getX() < currentMaxExtentLL.getX())

                currentMaxExtentLL.setX(layerFullExtentLL.getX());

        }

        mapView.setMaxExtent(new Envelope(

            currentMaxExtentLL.getX(),

            currentMaxExtentLL.getY(),

            currentMaxExtentUR.getX(),

            currentMaxExtentUR.getY()));

        if (zoomToMaxResolution) {

            mapView.zoomToResolution(mapView.getCenter(), mapView.getMinScale());

        }

        return currentMaxExtent;

    }

0 Kudos
ShellyGill1
Esri Contributor

One thing that occured to me is for things like map service layers, the full extent is taken directly from the service endpoint. I have often seen that the extent advertised in the service does not reflect accurately the exact spatial extent of the visible parts of the data (maybe due to changes, maybe due to other things, I'm not sure why it's the case). So the first thing is I would check the endpoints of the services and see if they exactly match with the visible parts of the layers you're displaying.

0 Kudos