Skip to content
Agents tracked: 440 Downloads (7d): 202M down 19.5% GitHub stars: 10.4M Releases (7d): 607 Agent status: 3 with issuesSince last limit reset: Claude 28d · Codex 0d Updated Oct 21, 2026

GeoJSON layers in Leaflet

GeoJSON is a common format for map data. Leaflet can turn a FeatureCollection of points, lines and polygons into map layers.

All Leaflet examples Leaflet: usage and stats

GeoJSON layers in Leaflet: screenshot of the example below running
The code below, running in Chromium on our server (Oct 12, 2026). Press Run to see it live and change it.

How it works

  1. Pass GeoJSON to L.geoJSON. data is a FeatureCollection with a polygon, a line and two points. L.geoJSON(data, options) creates a layer for each feature. [1]
  2. Style by property. The style function reads feature.properties.kind to choose a colour, and pointToLayer draws points as L.circleMarker. [1]
  3. Popups per feature. onEachFeature binds a popup with the feature name and geometry type. fitBounds then frames the whole layer. [1]

Run and edit it

It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.

import * as L from 'leaflet';

const el = document.createElement('div');
el.style.cssText = 'position:fixed;inset:0;background:#1d2633';
document.getElementById('root').appendChild(el);

const data = {
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', properties: { name: 'Riverside Park', kind: 'park' },
      geometry: { type: 'Polygon', coordinates: [[[-0.12, 51.5], [-0.1, 51.5], [-0.1, 51.51], [-0.12, 51.51], [-0.12, 51.5]]] } },
    { type: 'Feature', properties: { name: 'Bike route', kind: 'route' },
      geometry: { type: 'LineString', coordinates: [[-0.13, 51.497], [-0.09, 51.503], [-0.06, 51.499]] } },
    { type: 'Feature', properties: { name: 'Station', kind: 'stop' },
      geometry: { type: 'Point', coordinates: [-0.085, 51.507] } },
    { type: 'Feature', properties: { name: 'Market', kind: 'stop' },
      geometry: { type: 'Point', coordinates: [-0.07, 51.502] } },
  ],
};
const colors = { park: '#81c995', route: '#f6ae2d', stop: '#ff6b6b' };

const map = L.map(el);
const layer = L.geoJSON(data, {
  style: f => ({ color: colors[f.properties.kind], weight: 4, fillOpacity: 0.35 }),
  pointToLayer: (f, latlng) => L.circleMarker(latlng, { radius: 9, fillOpacity: 0.9 }),
  onEachFeature: (f, l) => l.bindPopup(`<b>${f.properties.name}</b><br>${f.geometry.type}`),
}).addTo(map);

map.fitBounds(layer.getBounds(), { padding: [30, 30] });
Ctrl/⌘ + Enter runs

Free for signed-in members, with a daily limit. Describe the change in plain words; the new code runs only in your browser.

Click the park, the route or a point to see its properties. Code written by AgentGid [1].

Checked Oct 12, 2026: it starts without errors in Chromium with leaflet 1.9.4. Needs WebGL.

More Leaflet examples

Markers and popups in Leaflet Handling map clicks in Leaflet Layer switcher and scale control in Leaflet

Sources

  1. Leaflet website

Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.

Building something with Leaflet? Ask AgentGid →