Javascript 传单js-marker.bindPopup-打开所有弹出窗口

Javascript 传单js-marker.bindPopup-打开所有弹出窗口,javascript,jquery,leaflet,Javascript,Jquery,Leaflet,我很难用传单打开所有的弹出窗口 我在a循环中有以下代码,用于将标记添加到LayerGroup(ajax自动更新) 它工作得很好,只是它只打开最后一个弹出窗口。我想把它们都打开。我确实在这里找到了一篇文章(stackoverflow),关于使用不同的标记名进行标记,但是我有一个循环。我确实试着把L.marker放进一个数组,但传单不喜欢这样 有什么想法吗?您需要覆盖传单地图上的openpopup方法,将其替换为此方法的副本,只注释掉调用此方法的行。closePopup() 在您的页面上,您可以添加

我很难用传单打开所有的弹出窗口

我在a循环中有以下代码,用于将标记添加到LayerGroup(ajax自动更新)

它工作得很好,只是它只打开最后一个弹出窗口。我想把它们都打开。我确实在这里找到了一篇文章(stackoverflow),关于使用不同的标记名进行标记,但是我有一个循环。我确实试着把L.marker放进一个数组,但传单不喜欢这样


有什么想法吗?

您需要覆盖传单地图上的openpopup方法,将其替换为此方法的副本,只注释掉调用此方法的行。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);        
    }
});

您可以在此处找到原始传单openPopup方法:

如果您希望一次渲染所有弹出窗口,则可以将弹出窗口渲染为单独的层。您不能在marker.bindpoppup(Popup)之后使用marker.openPopup(),因为传单会关闭当前打开的其他弹出窗口

我的React应用程序中有相同的任务

在示例中,this.api=L.map(this.map)

在弹出窗口中使用禁用参数禁用关闭按钮,并在弹出窗口外单击时防止关闭弹出窗口:

createPopup(point, disabled = false) {
    const {title, location} = point;

    const popup = L.popup({
      closeOnClick: !disabled,
      closeButton: !disabled,
      offset: [-22, -38]
    })
    .setLatLng(location)
    .setContent(title);

    return popup;
  }

正是我需要的。非常感谢。另一方面,我现在还有一个问题,当我将所有弹出窗口设置为自动打开,然后是map.fitBounds(),地图会缩小到一个奇怪的黑色/空白位置,如果我再次缩小,我会看到标记(但没有弹出窗口)。如果单击地图,弹出窗口将关闭
drawMarkers() {
const {points, keepAllPopupsOpen} = this.props;

this.markers.clearLayers();

points.forEach(point => {
  const hasPopup = point.title;
  const marker = this.createMarker(point, keepAllPopupsOpen);
  this.markers.addLayer(marker);

  if (hasPopup) {
    const popup = this.createPopup(point, keepAllPopupsOpen);

    if (keepAllPopupsOpen) {
      this.api.addLayer(popup);
    } else {
      marker.bindPopup(popup);
    }
  }
});
}
createPopup(point, disabled = false) {
    const {title, location} = point;

    const popup = L.popup({
      closeOnClick: !disabled,
      closeButton: !disabled,
      offset: [-22, -38]
    })
    .setLatLng(location)
    .setContent(title);

    return popup;
  }