Instead of using .on to capture and handle the event, you can use .once . Thus, the event will be recorded only once, and after that the handler will untie itself.
map.on('click', function () { console.log('I fire every click'); }); map.once('click', function () { console.log('I fire only once'); });
If you ever need to untie the handler yourself, you can use .off . Check out the link to event methods: http://leafletjs.com/reference.html#events
As for why your code above does not work, the first time you try to remove the marker: map.removeLayer(marker) , but the marker variable does not contain an instance of L.Marker, so the map cannot remove this. You need to check if this is defined first, and only then remove it:
var marker; map.on('click', function (e) { if (marker) {
Here is a working example on Plunker: http://plnkr.co/edit/iEcivecU7HGajQqDWzVH?p=preview
source share