javascript - 传单群控

标签 javascript leaflet

我有一个传单 map ,其中有多个图层LayerGroup和layerControl,如下图所示。 Original

我想知道如何仍然控制这些图层,但 Controller 不会停留在 map 上,而是停留在 map 之外,例如 enter image description here

最佳答案

您必须为 map 外部的 HTML 元素创建简单的事件监听器,以便它们在内部切换 Leaflet 图层。

例如像这样:

 <label><input type='checkbox' id='bicycles-ctrl' value='1'>Bicycles</label>
 // Init the map and its layers as needed
 var map = L.map(...);
 var bicyclesLayer = L.geoJson(...);


 // Let's attach a DOM event handler, the Leaflet way:
 L.domEvent.on(

      // Whenever the 'bicycles-ctrl' DOM element...
      document.getElementById('bicycles-ctrl')),

      // ...dispatches a 'change' event...
      'change',

      // ...run this function
      function(ev) {
          // The 'target' property of the DOM event points to the instance 
          // of HTMLInputElement which dispatched the event
          var input = ev.target;

          // The 'value' property will be empty (falsy) if the checkbox is unchecked,
          // or will have the contents of the 'value' HTML attribute (truthy)
          // otherwise.
          if (input.value) {
               // If the checkbox is checked, display the leaflet layer
               bicyclesLayer.addTo(map);
          } else {
               // If not, hide the leaflet layer
               bicyclesLayer.remove();
          }
      }
 );

具体的事件处理程序将取决于您的 HTML 标记和您想要实现的行为。

关于javascript - 传单群控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41752323/

相关文章:

javascript - 在传单中坐标为 Lat Long

javascript - 如何导出 .then() block 之外的数据(SuperAgent 库)?

javascript - 将弹出窗口/信息提示/工具提示显示为悬停特定文本

angularjs - 与 leaflet.heat 一起使用时,传单 CircleMarker 事件不起作用

Leaflet - 切换图层时缩放/平移到图层范围

shiny - 在 Shiny 中使用 includeHTML 显示 html 文件不适用于 renderUI()

javascript - Websockets 在 Chrome 中不工作 (wss) : “handshake was canceled”

javascript - div 的内容应始终以大写字母开头并以句号结尾

javascript - 图片库的淡入淡出问题

javascript - 以 getbounds 和相关问题为中心的 Leaflet map