Hi, when a marker is clicked, I need to execute some code that finds the id corresponding to the marker being clicked , retrieves data from backend API, then adds the newly retrieved data to the content of the popup that will open.
The only way that is able to listen to a click event on the marker is map.on('popupopen', function(e){}). How can I find out which marker this is? Is it possible to add an id attribute to each marker, then retrieve this id during the popupopen event?
Looks like you might want to check out this pull request?
https://github.com/CloudMade/Leaflet/pull/1010
e.popup._source is a reference to the marker that was clicked.
map.on('popupopen', function (e) {
e.popup._source.setIcon(new L.Icon({ iconUrl: 'https://secure.gravatar.com/avatar/798717b399b786a8fa7d1a94db8dc0a2?s=140&d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png' }));
});
To remember an id you can do something like:
var m = new L.Marker(...);
m._myId = 12345;
Then grab that out in the callback:
map.on('popupopen', function (e) {
alert(e.popup._source._myId);
});
Enjoy!
Most helpful comment
e.popup._source is a reference to the marker that was clicked.
To remember an id you can do something like:
Then grab that out in the callback: