GeoJSON Options
Quick answer
The GeoJSON options object controls how L.geoJSON converts data: pointToLayer chooses point classes, filter drops features early, style shapes lines and polygons, and onEachFeature attaches popups and listeners. Path styling values such as color, weight and fillOpacity pass straight through to lines and polygons. Tuning these callbacks avoids post-processing loops over generated layers.
L.geoJSON( geojson , options ) → GeoJSON
Key points
- style is ignored for points unless markersInheritOptions passes icon through to markers.
- filter returns false to exclude a feature before any layer is created.
- coordsToLatLng reorders coordinates when your source is not longitude first.
Example
L.geoJSON(data, {
filter: function (feature) {
return feature.properties.type !== 'hidden';
},
style: function (feature) {
var level = feature.properties.level;
return {
color: level > 2 ? '#c2410c' : '#1f5fd0',
weight: 1 + level * 0.6,
fillOpacity: 0.1 + level * 0.05
};
},
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, {
radius: 5,
color: '#fff',
weight: 1,
fillColor: '#17694a',
fillOpacity: 0.9
});
},
onEachFeature: function (feature, layer) {
if (!feature.properties.name) return;
layer.bindTooltip(feature.properties.name, { direction: 'top' });
}
}).addTo(map);
Notes
- Filtered features never generate layers, so onEachFeature cannot restore them.
- Filtering after load wastes memory; exclude features before they reach the renderer.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). GeoJSON Options. https://www.jingweizhi.com/en/leaflet/api/geojson-options.html (accessed 2026-11-03).