[GIS] Leaflet maxBounds doesn’t prevent zooming out

extentsleaflet

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.

Best Answer

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.