[GIS] Leaflet – map.fitBounds() / map.getBoundsZoom() BROKEN after resizing map

cartographyleaflet

https://jsfiddle.net/MaxHeadroom/s6h40xrn/

If you resize the map size, the zoom level should be adapted to always show the orange rectangle at maximum possible size.

But fitBounds seems to be broken after resizing the map.
Same problem with map.getBoundsZoom().

To reproduce the buggy behaviour, start with a small map size – increase it and decrease it then.
Check the console output too.

Or is there some a mistake in my source code?

Best Answer

You should call map.invalidateSize() after you change the map size. Or maybe try to wait

var corner1 = L.latLng(50.012, 8.227);
var corner2 = L.latLng(50.074, 8.125);
var bounds  = L.latLngBounds(corner1, corner2);
var rectangle = L.rectangle(bounds, {color: "#ff7800", weight: 3}).addTo(map);
function map_onResize(e){
    map.invalidateSize();
    //setTimeout(function(){
      map.fitBounds(rectangle.getBounds(), true);
    //},800)    
}