In Leaflet 1.0.2, if you set maxBounds on a map, it prevents panning beyond those bounds, but does not prevent zoom out beyond them. Is this by design? It seems incorrect behaviour to me. I need to prevent display of the map beyond given bounds, and thought maxBounds would do it.


This is by design, or rather said, by default. The devs (me included) thought "well, users already have minZoom to prevent this kind of thing", so the focus was on making sure that the maxBounds were always visible when zoomed out.

As a workaround, you might want to run something like:

 map.setMinZoom( map.getBoundsZoom( map.options.maxBounds ) );

Be aware that this might change if the map container is resized.


As another workaround, I used the properties said in here!, especifically that the width or height of the world is 256*Math.pow(2, zoomLevel) pixels.

Then, considering that the width and height of the map can be caught through javascript (clientWidth and clientHeight), the minZoom to not show beyond the bounds would be:

minZoom = Math.ceil(Math.log2(Math.max(width, height) / 256))

Which is just a development of the previous formula used with the map's width and height to find a minZoom value.

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.