Popup
Quick answer
L.popup renders a floating panel anchored to a map coordinate, usually opened by a bound layer's click, with a close button and optional auto pan. Standalone popups opened with openOn handle cases such as reporting coordinates for arbitrary map clicks.
L.popup( options? , latlng? , content? ) → Popup
Key points
- autoPan shifts the map so the popup stays inside the viewport when it opens.
- setContent followed by update is required for size and arrow recalculation.
- closeOnClick true closes the popup when the map is clicked elsewhere.
Example
var map = L.map('map', { center: [39.907359, 116.391263], zoom: 13 });
L.marker([39.907359, 116.391263])
.bindPopup('<b>天安门</b><br>39.907359, 116.391263', {
maxWidth: 240,
closeOnClick: true
})
.addTo(map);
/* 独立弹窗,不依赖图层 */
var popup = L.popup()
.setLatLng([39.92, 116.42])
.setContent('独立弹窗')
.openOn(map);
map.on('popupopen', function (e) {
console.log('已打开', e.popup.getLatLng());
});
Notes
- Content is parsed as HTML, so escaping untrusted values or passing DOM nodes is mandatory.
- autoClose true dismisses the previous popup, preventing several from being open at once.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Popup. https://www.jingweizhi.com/en/leaflet/api/popup.html (accessed 2026-11-03).