gpt4 book ai didi

javascript - Leaflet:如何在 map 上显示所有图层?

转载 作者:行者123 更新时间:2023-12-03 06:15:16 24 4
gpt4 key购买 nike

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

如何显示除名为“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>

最佳答案

只需执行与 "#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>

关于javascript - Leaflet:如何在 map 上显示所有图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39135100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com