Ask Your Question
4

What is the method for adding markers to a map with the Leaflet "click" event handler?

asked 2022-02-20 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-11-12 22:00:00 +0000

bukephalos gravatar image

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);
});
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-02-20 11:00:00 +0000

Seen: 9 times

Last updated: Nov 12 '22