問題描述
當(dāng)一個標(biāo)記被點(diǎn)擊時,我需要執(zhí)行一些代碼,找到與被點(diǎn)擊的標(biāo)記對應(yīng)的 id
,從后端 API 檢索數(shù)據(jù),然后將新檢索到的數(shù)據(jù)添加到 將打開的彈出窗口的內(nèi)容
.
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.
能夠監(jiān)聽標(biāo)記上的點(diǎn)擊事件的唯一方法是
The only way that is able to listen to a click event on the marker is
map.on('popupopen', function(e){
// How to retrieve marker?
// eg: Assign an id on creation, retrieve it now during popupopen
};)
我怎樣才能知道這是哪個標(biāo)記?是否可以為每個標(biāo)記添加 id
屬性,然后在 popupopen
事件期間檢索此 id
?
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?
推薦答案
事件對象包含一個popup"屬性,該屬性具有一個名為_source"的私有屬性,該屬性是彈出窗口綁定到的對象(即標(biāo)記).由于 _source 應(yīng)該是私有的,這似乎不是正確的方法,但我不知道該怎么做.
The event object contains a "popup" attribute that has a private attribute called "_source" which is the object that the popup is bound to (i.e. the marker). Since _source is supposed to be private this doesn't seem like the right way but I'm not sure how else to do it.
map.on('popupopen', function(e) {
var marker = e.popup._source;
});
這篇關(guān)于如何在“popupopen"事件期間識別 Leaflet 的標(biāo)記?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!