【问题标题】:Close popup in leaflet关闭传单中的弹出窗口
【发布时间】:2020-11-10 09:26:48
【问题描述】:

我有一个代码,它使用leaflet 将弹出窗口呈现给标记,如下所示。

const lat = e.latlng.lat;
   if ($('#lmap').css("cursor") === 'crosshair') {
      const note = `<div>popup<div>`;
      const newMarker = new L.marker(e.latlng,{draggable:true}).addTo(lmap);
      if (!(note === "")) {
         newMarker.bindPopup(note, {closeOnClick: false, autoClose: false, closeButton: true}).openPopup();
      }
   }

这里

closeButton: true 

用于在我的弹出窗口中呈现关闭按钮。但问题是当我点击标记时弹出关闭。如何防止在单击标记时关闭弹出窗口,而在单击弹出窗口中的关闭按钮时关闭它。

【问题讨论】:

    标签: javascript leaflet


    【解决方案1】:

    不要将弹出窗口直接添加到标记,创建一个弹出“层”并在单击标记时打开它:

    // a global variable
    var popup = L.popup({closeOnClick: false, autoClose: false, closeButton: true});
    
    
    //your function from above
    const newMarker = new L.marker(e.latlng,{draggable:true}).addTo(lmap);
    newMarker.on('click',(e)=>{
        popup.options.offset = e.target.options.icon.options.popupAnchor;
        popup.setContent('TEST').setLatLng(e.target.getLatLng()).addTo(map)
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多