11

In Openlayers I can zoom to the bounding box of all layers like this:

map.zoomToExtent();

I can also zoom to the bounding box of a single layer:

map.zoomToExtent(layer_name.getDataExtent());

Is there an easy way of zooming to the extent of all layers except the base layer? In other words showing the extent of all overlays.

Mr_Chimp
  • 3,773
  • 6
  • 37
  • 51

3 Answers3

7
  1. for-loop over all of your layers
    1. Skip the base layer [per your requirement]
    2. Use bounds.extend(...) to build the all-encompassing bounds object API
  2. Zoom to the resulting bounds API
Vadim
  • 3,971
  • 2
  • 29
  • 42
  • I think this won't work, because extends() only works if it is called on a smaller bounds than the one it is passed as argument. – pakman Jul 24 '14 at 18:59
  • How could i achieve same using OpenLayer API 3.x – imdadhusen Mar 12 '15 at 06:07
  • @imdadhusen why wouldn't you just look up the documentation? same method. same object. different namespace. http://openlayers.org/en/v3.3.0/apidoc/ol.extent.html#extend – Vadim Mar 13 '15 at 15:07
1

what about fixing a map.restrictedExtent and call map.zoomToExtent(map.restrictedExtent) ?

simo
  • 8,580
  • 1
  • 28
  • 56
  • That would just move the problem... How would I find out the bounding box of the overlays in order to create the restrictedExtent? – Mr_Chimp Feb 18 '11 at 14:30
  • have you try with ZoomToMaxExtent() - but it would probably zoom to base layer extent if larger? if it doesn't work you have to retrieve extent for each ovelays layers and keep the biggest one to fix restrictedExtent. – simo Feb 18 '11 at 14:43
  • I tried ZoomToMaxExtend() and it zooms to the base layer. I mentioned this in my question but on rereading it isn't obvious. Keeping the biggest bounding box wouldn't necessarily work - i.e. if two layers don't overlap. I guess I'll have to combine the bounding boxes of each layer somehow and then get the bounding box of the resulting object... – Mr_Chimp Feb 18 '11 at 14:56
  • That's it! Which is not that difficult ... But I agree with you, it's surprising such a method doesn't exist yet (or we missed something ;-)..) – simo Feb 18 '11 at 15:06
  • Well hopefully we've missed something! I'll leave the question open for now...just in case! – Mr_Chimp Feb 18 '11 at 15:12
  • There is maybe a way to do what you want with BBOX (http://dev.openlayers.org/docs/files/OpenLayers/Strategy/BBOX-js.html) but I never used it explicitly. – simo Feb 18 '11 at 15:20
0

This one worked for me (For layers). And this answer is the copy from stackoverflow

Code:

var allLayers = map.getLayers();
var length = allLayers.getLength();
var layerArray = ['Layer1', 'Layer2', ..., 'LayerN'];
var extent = ol.extent.createEmpty();
for (var i = 0; i < length; i++) {
    var existingLayer = allLayers.item(i);
    for (var j = 0; j < layerArray.length; j++) {
        if (existingLayer.get('title') == layerArray[j]) {
            ol.extent.extend(extent,existingLayer.getSource().getExtent());
        }
    }
}
map.getView().fit(extent, map.getSize());

Example:

I have added two layers and pointed to one layer among them.

Map snapshot with one layer in focus

After calling the API, my view updated and focused to both the layers:

Map snapshot with both the layer in focus

Basil
  • 139
  • 1
  • 7