2

May i ask how to I add a event listener to the map to detect when a marker has been added to the map? In the docs, it says that we can use the "add" event, but how do I actually use it? http://leafletjs.com/reference.html#marker

I have tried:

map.on(marker,'add', function() { // Throws a error "marker is not defined"
    console.log('marker added')
}); 

map.on('add', function() { // doesn't do anything
    console.log('marker added')
}); 

Any help?

2

The add event detects when a specific marker is added to the map.

var marker = L.marker([51.5, -0.09]); // first, define a marker

marker.on('add', function() { // called when the specific marker is added to the map
    console.log('marker added');
}); 

Demo: https://jsfiddle.net/wLqcsb0L/

I think you may want Leaflet's layeradd event on L.Map, which will fire when any layer is added to the map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.