Atlaslog
Atlaslog / Field Notes / Loading & display
Field note · Loading & display

Null geometry feature makes fitBounds throw null is not an object

Quick answer

GeoJSON allows geometry: null for features with attributes but no location. Leaflet turns those into empty layers without _bounds, and fitBounds throws while extending over them, aborting the rest of the init chain.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: Leaflet 1.9.x · turf.js 7.x · Mapbox GL JS 2.15
Symptom
Checklist
  1. Iterate the features and count how many have geometry === null.
  2. Call L.geoJSON(f).getBounds() on one null feature and confirm it returns empty bounds.
  3. Move fitBounds out of the init chain or wrap it in try/catch to see whether other features also fail.
  4. Trace the data source: null geometry usually comes from LEFT JOIN rows or fields that were blanked.
  5. Check turf.bbox on the same feature; it returns NaN rather than throwing.
Fix

Filter out features without geometry before they reach the render layer while keeping their properties for list views, or synthesise a point from lon/lat when a location is known, then call fitBounds on the filtered set.

Code
Uncaught TypeError: Cannot read properties of null (reading 'lat')
    at L.LatLngBounds.extend (leaflet.js:5)
    at L.Map.fitBounds (leaflet.js:5)
const nulls = fc.features.filter(f => f.geometry === null);
console.log(nulls.length);                   // 2
console.log(nulls[0].properties);            // { id: 517, name: '未定位点' }
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). Null geometry feature makes fitBounds throw null is not an object. https://www.jingweizhi.com/en/cookbook/geojson-null-geometry.html (accessed 2026-11-03).