Atlaslog / Field Notes / Leaflet containers & layers
Field note · Leaflet containers & layers
fitBounds does nothing for one point and zooms to the world for many
Quick answer
A single point produces a zero-area bounds, so the computed zoom runs to infinity and is clamped; an empty array or swapped lat/lng makes the bounds invalid or span half the globe. Either way the view does not move or collapses to zoom 2.
Symptom
- bounds.getSouthWest() and getNorthEast() both return LatLng {lat: 39.907, lng: 116.391}, the same corner twice.
- A multi-point fitBounds ends at zoom 2 with the entire world visible.
- No error is thrown in either case.
Checklist
- Print bounds.toBBoxString(), map.getSize() and getZoom() before and after the call.
- Check whether the bounds is a single point or empty.
- Check whether coordinates were passed as [lng, lat] instead of [lat, lng].
- Check map.getSize().y for zero and whether a flyTo animation is still running.
Fix
Use map.setView(latlng, 15) for single points, or pad the point into a 0.002 degree box and fitBounds with { padding: [40, 40], maxZoom: 16 }, calling invalidateSize first when the container is zero-sized.
Code
console.log(bounds.isValid()); // true
console.log(bounds.getSouthWest(), bounds.getNorthEast());
// LatLng {lat: 39.907, lng: 116.391} LatLng {lat: 39.907, lng: 116.391} ← 两个角相同console.log('bounds', bounds.toBBoxString());
console.log('size', map.getSize());
console.log('zoom before', map.getZoom());
map.fitBounds(bounds);
console.log('zoom after', map.getZoom());
Watch out
- Passing maxZoom without padding leaves markers at the edge half outside the viewport.
- Calling fitBounds repeatedly during a flyTo animation merges the calls and the final view is not the requested one.
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). fitBounds does nothing for one point and zooms to the world for many. https://www.jingweizhi.com/en/cookbook/leaflet-fitbounds-no-effect.html (accessed 2026-11-03).