I need to get the bounds of all layers in a Leaflet map, not just vector layers. For vectors, I add them to a L.featureGroup, which has the method getBounds(). How do I include raster layers as well?

  • What kind of raster layers do you use? – ghybs Jan 6 '16 at 14:02
  • In the first instance, I'm using L.imageOverlays. – Tom Chadwin Jan 6 '16 at 15:23

In the case of raster layers being L.imageOverlay's, you must have instantiated them specifying the image bounds.

If for any reason you lost reference of these bounds, you can retrieve them using myImageOverlay._bounds.

We could even imagine extending the L.ImageOverlay class to include a getBounds() method, so that it becomes compatible when embedded in a Feature Group with the latter own getBounds() method:

L.ImageOverlay.include({ // to be included in your script before instantiating image overlays.
    getBounds: function () {
        return this._bounds;

var myImageOverlay = L.imageOverlay(imageUrl, bounds);

var myFeatureGroup = L.featureGroup([myImageOverlay]);

var myBounds = myFeatureGroup.getBounds();
  • This looks like a brilliant solution. I'll try it out. – Tom Chadwin Jan 6 '16 at 20:05
  • Yes, that works perfectly (extending L.ImageOverlay with .getBounds()) - thanks so much. – Tom Chadwin Jan 6 '16 at 23:05

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.