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

Handling map clicks in Leaflet

Map events let an app respond to the user, for example to read the coordinates of a clicked point or track the zoom level.

All Leaflet examples Leaflet: usage and stats

Handling map clicks 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. Listen with map.on. map.on('click', handler) runs the handler for every click on the map. The event's latlng holds the clicked coordinates. [1]
  2. Draw at the clicked point. The handler adds an L.circleMarker at e.latlng and writes the rounded latitude and longitude into the info box. [1]
  3. React to zoom. A zoomend listener updates the zoom level shown in the box after scroll-wheel, double-click or button zooms. [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 info = document.createElement('div');
info.style.cssText = 'position:fixed;top:10px;left:50%;transform:translateX(-50%);z-index:1000;' +
  'background:#fff;color:#111;padding:6px 12px;border-radius:6px;font:14px system-ui,sans-serif';
document.getElementById('root').appendChild(info);

const map = L.map(el).setView([51.505, -0.09], 14);
L.rectangle([[51.5, -0.105], [51.51, -0.075]], { color: '#8ab4f8', weight: 1, fillOpacity: 0.1 }).addTo(map);

let last = 'Click anywhere on the map';
const show = () => { info.textContent = `${last}  |  zoom ${map.getZoom()}`; };
show();

map.on('click', e => {
  const { lat, lng } = e.latlng;
  L.circleMarker(e.latlng, { radius: 7, color: '#ff6b6b', fillOpacity: 0.8 }).addTo(map);
  last = `Clicked ${lat.toFixed(4)}, ${lng.toFixed(4)}`;
  show();
});

map.on('zoomend', show);
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 anywhere to drop a point and read its coordinates; zoom to see the level change. 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 GeoJSON layers 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 →