問題描述
我在使用傳單打開所有彈出窗口時遇到了一些困難.
I am having some difficulty keeping all the popups open with leaflet.
我在 a 循環中有以下代碼,用于向 LayerGroup 添加標記(ajax 自動更新).
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);
}
});
http://jsfiddle.net/yVLJf/37/
您可以在此處找到原始 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 - 保持所有彈出窗口打開的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!