Adding Leaflet layer control to sidebar

痞子三分冷 提交于 2020-01-13 05:18:06

问题


I'm using the Leaflet sidebar V2 plugin and am wondering if it is possible to put a leaflet layer control menu into the sidebar? Any help is greatly appreciated! Thanks!


回答1:


Just use this hint. It removes the L.control element from the map container and adds it to a new parent. You can do this for the sidebar straight forward.

HTML:

 <div id="sidebar" class="sidebar collapsed">
   <div class="sidebar-tabs">
    <ul role="tablist">
     <li>....</li>
     <li>....</li>
    </ul>
 </div>
 <div class="sidebar-content">
   <div class="sidebar-pane" id="">  
    <h1 class="sidebar-header">...</h1>
    <div id="example"> <!--Here the layer control menu will be added--></div>    
 </div>
</div>

JS:

var layer = L.control.layers(baseMaps, overlayMaps).addTo(map);

  var htmlObject = layer.getContainer();
  var a = document.getElementById('example')
  function setParent(el, newParent){
    newParent.appendChild(el);
  }
  setParent(htmlObject, a);

For me, this solution worked well.



来源:https://stackoverflow.com/questions/41475855/adding-leaflet-layer-control-to-sidebar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!