javascript 如何在“popupopen”事件期间识别传单的标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12701240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to identify Leaflet's Marker during a `popupopen` event?
提问by Nyxynyx
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.
单击标记时,我需要执行一些代码来查找id
与被单击的标记对应的标记,从后端 API 检索数据,然后将新检索到的数据添加到content
将打开的弹出窗口的 。
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
};)
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?
我怎样才能找出这是哪个标记?是否可以为id
每个标记添加一个属性,然后id
在popupopen
事件期间检索它?
回答by InPursuit
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.
事件对象包含一个“popup”属性,该属性具有一个名为“_source”的私有属性,该属性是弹出窗口绑定到的对象(即标记)。由于 _source 应该是私有的,这似乎不是正确的方法,但我不知道该怎么做。
map.on('popupopen', function(e) {
var marker = e.popup._source;
});
回答by datashaman
Javascript objects can have any properties defined on them. Set popup.marker to the referenced marker when you create the popup. Then you can access it later in the event handler.
Javascript 对象可以定义任何属性。创建弹出窗口时,将 popup.marker 设置为引用的标记。然后您可以稍后在事件处理程序中访问它。
回答by Waseem Abu Senjer
To get the marker id, you can use this code:
要获取标记 ID,您可以使用以下代码:
map.on('popupopen', function(e) {
var marker = e.popup._source.feature.properties.markerid;
});
回答by Jan Kyu Peblik
Other answers didn't work, but this does:
其他答案不起作用,但这确实有效:
map.on('popupopen', function(e) { alert(e.popup._source._popup._content); });
Guess this library is pretty volatile ...and I'm not sure why it's this complicated to transmit such information in the first place. <shrug>
猜猜这个库非常不稳定……而且我不确定为什么首先传输此类信息会如此复杂。<耸肩>