我试图在加载时显示每个 map 标记的工具提示,而无需悬停或单击即可显示它。这是我尝试将 openPopup 函数链接到 bindPopup 的尝试:
function onEachFeature(feature, layer) {
if (feature.properties && feature.properties.popupContent) {
popupContent = feature.properties.popupContent;
}
layer.bindPopup(popupContent).openPopup();
}
但是除非单击,否则工具提示不会出现。
我看到this page文档提供了以下功能,但它仅适用于单个标记,而不是多个标记。
marker.eachLayer(function(m) {
m.openPopup();
});
如何在加载时显示所有标记?
最佳答案
不幸的是,这就是传单中弹出窗口的工作方式。
https://stackoverflow.com/a/16707921/128165 中提供了一个小技巧。
/*** little hack starts here ***/
L.Map = L.Map.extend({
openPopup: function (popup) {
// this.closePopup(); // just comment this
this._popup = popup;
return this.addLayer(popup).fire('popupopen', {
popup: this._popup
});
}
}); /*** end of hack ***/
将其添加到代码中后,您就可以使用
for (var o in overlays){
overlays[o].eachLayer(function (m) {
m.eachLayer(function(l){l.openPopup();});
});
}
迭代您案例中的所有标记并调用其 openPopup
方法
关于javascript - Mapbox在加载时显示多个工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24210943/