宣传单:如何在地图上显示所有图层?

时间:2016-08-25 00:53:47

标签: javascript leaflet

我正在尝试创建一个按钮,单击该按钮将在所有图层上切换,除了某些特定图层。我无法完成这项工作。

除了名为'stuff'和'stuff2'的叠加层外,我如何显示所有图层?

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);
var stuff = L.marker([48.83602344356167,2.3617172241210938]).addTo(map);
var stuff2 = L.marker([48.840542852103084,2.3246383666992183]).addTo(map);

$("#clearAll").click(function(event) {
  event.preventDefault();

  $(".check").each(function(i, el) {
  	el.checked = false; // Set new status (unchecked) first.
    $(el).change(); // Trigger the event.
  })
});

$(".check").change(function() {
  var layerClicked = $(this).attr("id");
  switch (layerClicked) {
    case "airfields":
      toggleLayer(this.checked, airfields);
      break;
    case "docks":
      toggleLayer(this.checked, docks);
      break;
    case "stuff": toggleLayer(this.checked, stuff);
      break;
      case "stuff2": toggleLayer(this.checked, stuff2);
      break;
      // ...and so on...
  }
});

function toggleLayer(checked, layer) {
	if (checked) {
  	map.addLayer(layer);
  } else {
  	map.removeLayer(layer);
  }
}
#map {
  height: 300px;
}
<script src="http://cdn.leafletjs.com/leaflet/v1.0.0-rc.1/leaflet.js"></script>
<link href="http://cdn.leafletjs.com/leaflet/v1.0.0-rc.1/leaflet.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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 />
<input type="checkbox" id="stuff" class="check" checked>stuff
<br />
<input type="checkbox" id="stuff2" class="check" checked>stuff2
<br />
<button id="clearAll">
  Clear All
</button>
<button id="showAll">
  Show All
</button>

1 个答案:

答案 0 :(得分:1)

只需在"#clearAll"中执行相同操作,但强制启用一些复选框,关闭其他复选框("stuff""stuff2"

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);
var stuff = L.marker([48.83602344356167,2.3617172241210938]).addTo(map);
var stuff2 = L.marker([48.840542852103084,2.3246383666992183]).addTo(map);

$("#showAll").click(function(event) {
  event.preventDefault();

  $("#airfields")[0].checked = true;
  $("#docks")[0].checked = true;
  $("#stuff")[0].checked = false;
  $("#stuff2")[0].checked = false;

  $(".check").each(function(i, el) {
    $(el).change(); // Trigger the event.
  })
});

$("#clearAll").click(function(event) {
  event.preventDefault();

  $(".check").each(function(i, el) {
    el.checked = false; // Set new status (unchecked) first.
    $(el).change(); // Trigger the event.
  })
});

$(".check").change(function() {
  var layerClicked = $(this).attr("id");
  switch (layerClicked) {
    case "airfields":
      toggleLayer(this.checked, airfields);
      break;
    case "docks":
      toggleLayer(this.checked, docks);
      break;
    case "stuff":
      toggleLayer(this.checked, stuff);
      break;
    case "stuff2":
      toggleLayer(this.checked, stuff2);
      break;
      // ...and so on...
  }
});

function toggleLayer(checked, layer) {
	if (checked) {
  	map.addLayer(layer);
  } else {
  	map.removeLayer(layer);
  }
}
#map {
  height: 300px;
}
<script src="http://cdn.leafletjs.com/leaflet/v1.0.0-rc.1/leaflet.js"></script>
<link href="http://cdn.leafletjs.com/leaflet/v1.0.0-rc.1/leaflet.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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 />
<input type="checkbox" id="stuff" class="check" checked>stuff
<br />
<input type="checkbox" id="stuff2" class="check" checked>stuff2
<br />
<button id="clearAll">
  Clear All
</button>
<button id="showAll">
  Show All
</button>