Atlaslog
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.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: Leaflet
Symptom
Checklist
  1. Print bounds.toBBoxString(), map.getSize() and getZoom() before and after the call.
  2. Check whether the bounds is a single point or empty.
  3. Check whether coordinates were passed as [lng, lat] instead of [lat, lng].
  4. 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
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

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).