問題描述
當一個標記被點擊時,我需要執行一些代碼,找到與被點擊的標記對應的 id
,從后端 API 檢索數據,然后將新檢索到的數據添加到 將打開的彈出窗口的內容
.
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 to retrieve marker?
// eg: Assign an id on creation, retrieve it now during popupopen
};)
我怎樣才能知道這是哪個標記?是否可以為每個標記添加 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"的私有屬性,該屬性是彈出窗口綁定到的對象(即標記).由于 _source 應該是私有的,這似乎不是正確的方法,但我不知道該怎么做.
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;
});
這篇關于如何在“popupopen"事件期間識別 Leaflet 的標記?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!