如何在“popupopen"事件期间识别 Leaflet 的标记?

2022-01-12 00:00:00 gis openstreetmap leaflet jquery javascript

当一个标记被点击时,我需要执行一些代码,找到与被点击的标记对应的 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;
});

相关文章