Leafletjs - marker.bindPopup - 保持所有弹出窗口打开

2022-01-12 00:00:00 leaflet jquery javascript

我在使用传单打开所有弹出窗口时遇到了一些困难.

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

相关文章