clear all layers AND clear input checkboxes? - Javascript Leaflet

Javascript examples for Leaflet:Layer

Description

clear all layers AND clear input checkboxes?

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://code.jquery.com/jquery-1.9.1.js"></script> 
      <link rel="stylesheet" type="text/css" href="http://cdn.leafletjs.com/leaflet/v1.0.0-rc.1/leaflet.css"> 
      <script type="text/javascript" src="http://cdn.leafletjs.com/leaflet/v1.0.0-rc.1/leaflet.js"></script> 
      <style id="compiled-css" type="text/css">
#map {/*from   ww  w  . j a v a  2  s  .co  m*/
   height: 300px;
}
      </style> 
      <script type="text/javascript">
    $(window).load(function(){
var map = L.map("map");
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png").addTo(map);
map.setView([48.85, 2.35], 11);
var airfields = L.marker([48.85, 2.35]).addTo(map);
var docks = L.marker([48.85, 2.33]).addTo(map);
$("#clearAll").click(function(event) {
  event.preventDefault();
  $(".check").each(function(i, el) {
    if (el.checked) {
         $(el).change();
      el.checked = false;
    }
  })
});
$(".check").change(function() {
  var layerClicked = $(this).attr("id");
  switch (layerClicked) {
    case "airfields":
      toggleLayer(airfields);
      break;
    case "docks":
      toggleLayer(docks);
      break;
  }
});
function toggleLayer(layer) {
  if (map.hasLayer(layer)) {
    map.removeLayer(layer);
  } else {
    map.addLayer(layer);
  }
}
    });

      </script> 
   </head> 
   <body> 
      <div id="map"></div> 
      <br> 
      <input type="checkbox" id="airfields" class="check" checked>
      Airfields 
      <br> 
      <input type="checkbox" id="docks" class="check" checked>
      Docks 
      <br> 
      <button id="clearAll"> Clear All </button>  
   </body>
</html>

Related Tutorials