MapSnippets logoMapSnippets logo

Add Markers and Popups to a Leaflet Map

Leaflet

Place L.marker pins on a Leaflet map with interactive popups containing custom HTML content.

progress_activityLoading map...
const map = L.map('map').setView([48.8566, 2.3522], 13);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', {
  tileSize: 256,
  attribution: '&copy; <a href="https://www.maptiler.com/">MapTiler</a> &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
L.marker([48.8584, 2.2945]).addTo(map).bindPopup('<strong>Eiffel Tower</strong><br>Champ de Mars, Paris');
L.marker([48.8606, 2.3376]).addTo(map).bindPopup('<strong>Louvre Museum</strong><br>Rue de Rivoli, Paris');
L.marker([48.8530, 2.3499]).addTo(map).bindPopup('<strong>Notre-Dame</strong><br>Île de la Cité, Paris');

How it works

L.marker() creates a marker at the given [lat, lng] coordinates and adds it to the map with .addTo(map). Each marker can have a popup attached via .bindPopup(), which accepts an HTML string for rich content like bold text, links, or images.

Popups open when the user clicks the marker. You can also call .openPopup() to show it programmatically. Only one popup is open at a time by default: clicking a different marker closes the previous one.

Key APIs

API Description Docs
L.marker() Create a marker leafletjs.com/reference.html#marker
.bindPopup() Attach popup to layer leafletjs.com/reference.html#layer-bindpopup
.openPopup() Open popup programmatically leafletjs.com/reference.html#layer-openpopup

FAQ

How do I use a custom marker icon?

Pass an L.icon() instance in the marker options: L.marker([lat, lng], { icon: L.icon({ iconUrl: 'pin.png', iconSize: [25, 41] }) }).

Can I have multiple popups open at the same time?

Yes. Set autoClose: false and closeOnClick: false in the popup options: .bindPopup('text', { autoClose: false, closeOnClick: false }).

Common mistakes

  • Using [lng, lat] instead of [lat, lng]: Leaflet coordinate order is the opposite of GeoJSON
  • Forgetting .addTo(map): the marker is created but never added to the map
  • Putting unescaped quotes in popup HTML: use single quotes inside double-quoted strings or vice versa
auto_awesome

Build maps using Leaflet with AI

Generate code like this add markers and popups to a leaflet map example using the Leaflet skill in your IDE.