William Entriken
William Entriken

Reputation: 39263

Leaflet to select a location

I would like to use Leaflet to have the user input lat long information. Should I be creating and deleting markers and calculating the lat-long of each click/touch by myself or is there a cleaner way for the user to be able to select a location?

Upvotes: 4

Views: 4327

Answers (1)

pk.
pk.

Reputation: 986

Leaflet MouseEvent's already contain latlng information. From there you should be able to add and remove markers during each click event.

var marker;
map.on('click', function(e) {
    if(marker)
        map.removeLayer(marker);
    console.log(e.latlng); // e is an event object (MouseEvent in this case)
    marker = L.marker(e.latlng).addTo(map);
});

Upvotes: 5

Related Questions