gpt4 book ai didi

javascript - 在 layerGroup 上打开传单弹出窗口

转载 作者:行者123 更新时间:2023-11-29 10:42:22 25 4
gpt4 key购买 nike

我正在尝试使用 L.GeoJSON(data).addTo(map) 在传单 map 上绘制国家/地区形状。然后我想将弹出窗口绑定(bind)到该国家/地区形状的点击事件...

new L.GeoJSON(data, {
onEachFeature: function(feature, layer) {
layer['on']('click', popupFunction);
}
}).addTo(this.map);


popupFunction = function(event) {
var layer = event.target;

// Open the 'add' popup and get our content node
var bound = layer.bindPopup(
"<div>Hello World!</div>"
).openPopup();

// Ugly hack to get the HTML content node for the popup
// because I need to do things with it
var contentNode = $(bound['_popup']['_contentNode']);
}

现在,当数据是单个多边形时,这可以正常工作,因为传递给 onEachFeature 函数的 layer 属性就是:一个层。

但是,如果 data 是一个多面体(即美国),这将停止工作,因为“layer”现在是一个 layerGroup(它有一个 _layers) 属性,因此没有 _popup 属性,因此我无法获取弹出窗口的 _contentNode

看起来这应该是很常见的事情,想要在 layerGroup 上弹出。为什么它没有 _popup 属性?

最佳答案

简答:图层组不支持弹出

B计划:

你应该考虑使用 FeatureGroup , 它扩展了 LayerGroup并具有 bindPopup 方法,这是一个示例

L.featureGroup([marker1, marker2, polyline])
.bindPopup('Hello world!')
.on('click', function() { alert('Clicked on a group!'); })
.addTo(map);

关于javascript - 在 layerGroup 上打开传单弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26241377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com