javascript - 使用传单中的测量工具时禁用弹出窗口

标签 javascript jquery leaflet

我在传单中使用了一个测量插件工具,但是当我尝试在标记之间进行测量时,弹出窗口会干扰有没有办法解决这个问题?我阅读了一些关于 oddclicks 的内容,我尝试使用它但无济于事。

$(".leaflet-control-measure").click(function() {
  var oddClick = $(this).data("oddClick");
 $(this).data("oddClick", !oddClick);
 if (!oddClick) {
   map.off('click', popup);
 } else {
    map.on('click', popup);
 }
 });

enter image description here

   popup logic- I am reading from a database, the popup is called from python in a for loop, and rendered using the jinja2 template
 var markers= L.markerClusterGroup({
      disableClusteringAtZoom: 15,
      minZoom : 2

  });


  {% for item in markers %}


   var resortIcon = L.icon({
       iconUrl: '{{ item[3] }}',
      iconSize: [25, 25],
      popupAnchor: [0,-15]
    });

   var marker{{ item[0] }} = L.marker({{ item[5:] }}, {icon: resortIcon});
   var popup = "<table height='90'><tr><td>{{ item[1] }}</td></tr><tr><td 
 align='center'><b><i>{{ item[4] }}</b></i></td></tr><tr><td align='center'> 
   <a href='www.google.com' onmouseover='More info'><img src='../icon/contract.svg' height=30 width=30 /></a></td></tr></table>";
  marker{{ item[0] }}.bindPopup(popup);
  markers.addLayer(marker{{ item[0] }});
  map.addLayer(markers)


  {% endfor %}

最佳答案

如果我没理解错的话,您是想在测量时阻止弹出窗口出现。我绝不是 leaflet 方面的专家,因为我以前从未使用过它,但是通过一些文档的挖掘,这似乎是你最好的选择。

本质上,您可以将事件处理程序绑定(bind)到每个标记上的 popupopen 事件,并且在该处理程序内部,如果满足某些条件(即启用“测量模式”),您可以立即关闭弹出窗口).

它是这样工作的:

var startingCoords = [40.07573, -105.401047];
var markerCoords = [
  startingCoords,
  [40.512318, -105.665104],
  [39.825169, -104.994123],
];
var map = L.map('map').setView(startingCoords, 8);
var enablePopups = document.getElementById("enablePopups");
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

function onPopupOpen(e) {
  // check if you it is okay for the popup to open - if not, close it.
  if (!enablePopups.checked) {
    // "this" refers to the marker object of the marker that was clicked on, whereas
    // e.target will refer to the DOM element itself
    this.closePopup();
  }
}

// loop to create each marker
markerCoords.forEach(function (coords) {
  var marker = L.marker(coords).addTo(map).bindPopup('Location at [' + coords + ']');
  // use .bind(marker) so we can access the marker itself via "this" inside the onPopupOpen
  // handler. That way, we don't have to keep an array of markers as a reference - each one
  // is self-referencing.
  marker.on("popupopen", onPopupOpen.bind(marker));
});
#map {
  width: 500px;
  height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
Enable Popups ? <input type="checkbox" checked id="enablePopups" />
<p>Click on a marker with "enable popups" checked vs unchecked - you can manually disable the popups based on a condition</p>
<div id="map"> </div>

关于javascript - 使用传单中的测量工具时禁用弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630832/

相关文章:

javascript - 如何暂时禁用事件?

javascript - 使用 cookie 的最佳实践

包含数百万行的 JavaScript 数据网格

javascript - 使用 Mapbox 或 Leaflet 突出显示多边形

javascript - 按 id 拆分 div 内容以检查字符串的长度

javascript - 重新调整 Flash 对象的大小无法正常工作

javascript - 使用选择器单击时记录

javascript - 是否可以检查变量是否等于窗口对象?

reactjs - 在 react 传单 map 上自动打开标记弹出

javascript - 传单触摸事件未触发