首页 > 解决方案 > 关闭传单中的弹出窗口

问题描述

我有一个代码,它使用一个标记来呈现一个弹出窗口,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 

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

标签: javascriptleaflet

解决方案


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

// 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)
})

推荐阅读