GeoJSON
Quick answer
L.geoJSON turns GeoJSON feature collections into Leaflet layers, inheriting every FeatureGroup capability plus pointToLayer, style, filter and onEachFeature hooks. It is the standard way to load vector data and to export it back with toGeoJSON at a chosen precision. Each generated child layer is reachable through eachLayer and getLayers.
L.geoJSON( geojson? , options? ) → GeoJSON
Key points
- pointToLayer decides which layer class a Point becomes; markers are the default.
- style callbacks affect only lines and polygons, never point features.
- addData appends more GeoJSON and re-runs filter, style and onEachFeature.
Example
var layer = L.geoJSON(data, {
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, { radius: 6, fillOpacity: 0.9 });
},
style: function (feature) {
return { color: '#1f5fd0', weight: 2, fillOpacity: 0.2 };
},
onEachFeature: function (feature, lyr) {
lyr.bindPopup(feature.properties.name);
}
}).addTo(map);
layer.addData({
type: 'Feature',
properties: { name: '新增点' },
geometry: { type: 'Point', coordinates: [116.41, 39.91] }
});layer.eachLayer(function (lyr) {
lyr.on('click', function () {
layer.eachLayer(function (other) { layer.resetStyle(other); });
lyr.setStyle({ color: '#c2410c', weight: 3 });
});
});
Notes
- Features whose geometry is null are skipped silently, so validate data upstream.
- Coordinates outside plus or minus 180 and 90 degrees are not clipped before rendering.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). GeoJSON. https://www.jingweizhi.com/en/leaflet/api/geojson.html (accessed 2026-11-03).