Leaflet: How to identify Marker during a `popupopen` event?

Created on 3 Oct 2012  路  2Comments  路  Source: Leaflet/Leaflet

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?

Most helpful comment

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!

All 2 comments

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!
Was this page helpful?
0 / 5 - 0 ratings

Related issues

brambow picture brambow  路  3Comments

edmsgists picture edmsgists  路  3Comments

zdila picture zdila  路  3Comments

arminghm picture arminghm  路  3Comments

walterfn2 picture walterfn2  路  4Comments