leafletjs - marker.bindPopup - 保持所有弹出窗口打开 [英] leafletjs - marker.bindPopup - keep all popups open
问题描述
我很难用传单打开所有弹出窗口。
I am having some difficulty keeping all the popups open with leaflet.
我在a循环中有以下代码将标记添加到LayerGroup(ajax auto-更新)。
I have the following code in the a loop to add markers to a LayerGroup (ajax auto-updating).
var marker = L.marker([value.position.lat, value.position.lon],options).addTo(markers);
allpoints.push([value.position.lat, value.position.lon]);
marker.bindPopup("InfoWindow",{closeOnClick:false,closeButton:false}).openPopup();
它工作得很好,除了它只保持最后一个弹出窗口打开。我想保持所有这些都开放。我确实在这里找到了一篇关于使用不同标记名称的文章(stackoverflow),但是我在循环中有这个。我确实尝试将L.marker放入一个数组,但传单不喜欢。
It works great, except it only keeps the last popup open. I would like to keep all of them open. I did find an article on here (stackoverflow) regarding doing so with different marker names, however I have this in a loop. I did try putting L.marker into an array, but leaflet did not like that.
任何想法?
推荐答案
您需要覆盖Leaflet Map上的openpopup方法,将其替换为此方法的副本,仅注释掉调用this.closePopup()的行;
You will need to override the openpopup method on the Leaflet Map, replacing it with a copy of this method, only comment out the line that calls this.closePopup();
在你的页面上你会添加
L.Map = L.Map.extend({
openPopup: function (popup, latlng, options) {
if (!(popup instanceof L.Popup)) {
var content = popup;
popup = new L.Popup(options).setContent(content);
}
if (latlng) {
popup.setLatLng(latlng);
}
if (this.hasLayer(popup)) {
return this;
}
// NOTE THIS LINE : COMMENTING OUT THE CLOSEPOPUP CALL
//this.closePopup();
this._popup = popup;
return this.addLayer(popup);
}
});
您可以在此处找到原始的Leaflet openPopup方法:
https://github.com/Leaflet/Leaflet/blob/1acffc5a3d31010b7d613382ab2a5830ecee5dd5 /src/layer/Popup.js#L290
You can find the original Leaflet openPopup method here: https://github.com/Leaflet/Leaflet/blob/1acffc5a3d31010b7d613382ab2a5830ecee5dd5/src/layer/Popup.js#L290
这篇关于leafletjs - marker.bindPopup - 保持所有弹出窗口打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!