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


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


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.

3 Answers 3

  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
  • 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, 2014 at 18:59
  • How could i achieve same using OpenLayer API 3.x
    – imdadhusen
    Mar 12, 2015 at 6:07
  • @imdadhusen why wouldn't you just look up the documentation? same method. same object. different namespace. openlayers.org/en/v3.3.0/apidoc/ol.extent.html#extend
    – Vadim
    Mar 13, 2015 at 15:07

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

  • 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, 2011 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, 2011 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, 2011 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, 2011 at 15:06
  • Well hopefully we've missed something! I'll leave the question open for now...just in case!
    – Mr_Chimp
    Feb 18, 2011 at 15:12

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


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]) {
map.getView().fit(extent, map.getSize());


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.