Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method for adding markers to a map with the Leaflet "click" event handler is:

  1. Create a new marker object using the L.marker() function.
  2. Set the marker's position to the clicked location using the event.latlng property.
  3. Add the marker to the map using the map.addLayer() function.
  4. Optional: You can also add popup content to the marker using the marker.bindPopup() function.

Here is an example code snippet:

map.on('click', function(event) {
  var marker = L.marker(event.latlng);
  // add popup content
  marker.bindPopup("<b>Hello World!</b><br>I am a popup.").openPopup();
  map.addLayer(marker);
});