Leaflet layer control open only on click - Javascript Leaflet

Javascript examples for Leaflet:Layer

Description

Leaflet layer control open only on click

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
      <link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css"> 
      <script type="text/javascript" src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script> 
      <style id="compiled-css" type="text/css">

#map{//from w w w.  j a  v  a 2s .  com
   height:500px;
   width:500px;
}
#btn{
   position:absolute;
   top:10px;
   right:10px;
   z-index:1000;
   background:white;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.marker([51.5, -0.09]).addTo(map);
var first = L.layerGroup().addTo(map);
var second = L.layerGroup().addTo(map);
var baseLayers = {
  };
  var overlays = {
    "first": first,
    "second": second
  };
  L.control.layers(baseLayers,overlays,{collapsed:false}).addTo(map);
  $('.leaflet-control-layers').hide();
  $('.leaflet-control-layers').css('top','30px');
  btn.onclick = function() {
     $('.leaflet-control-layers').toggle();
  }
    }

      </script> 
   </head> 
   <body> 
      <div id="map"> 
         <button id="btn">layers</button> 
      </div>  
   </body>
</html>

Related Tutorials