OpenLayers 2 – How to Zoom to Extent of All Overlays

openlayers-2

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.

Best Answer

  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