지도에 Leaflet.js를 사용하고 있습니다. 이제지도에서 추가 된 레이어를 제거하고 싶습니다. 입력 # 버튼을 클릭하면 체크 된 모든 체크 박스가 체크되지 않은 상태로 변경되고 모든 해당 레이어가 맵에서 제거됩니다.Leaflet.js :지도에서 여러 레이어를 제거하는 방법
지도에서 레이어를 제거하려면 레이어의 ID가 필요합니다. 이 id는 해당 확인란의 id와 같습니다. 그래서 jQuery를 사용하여 체크 된 모든 체크 박스의 ID를 가져 와서 객체에 값을 저장합니다. 여기에는 someObj.idsChecked이라는 값이 있습니다.
나는 그것이 을 console.log 표시하면서 원하는 값을 작동하지 않는 하나 개의 층을 제거하기 위해 저장된 값 발를 사용하려고하면. 여기 예 : mapcat52.
map.removeLayer (mapcat52)과 같이 함수에 하드 코딩 된 이전 ID를 삽입하는 동안 예상대로 작동합니다.
내 코드 또는 내 생각에 오류가 어디 있습니까?
도움을 주시면 감사하겠습니다.
html로
<input type="button" id="selectnone" value="deselect all" />
<!-- checkboxes -->
<input id="mapcat52" type="checkbox" name="maplayer" class="maplayer">
<label for="mapcat52">Map Layer One</label>
<input id="mapcat53" type="checkbox" name="maplayer" class="maplayer">
<label for="mapcat53">Map Layer Two</label>
...
JS :
$('#selectnone').click(function() {
var someObj = {};
someObj.idsChecked = [];
$("input:checkbox").each(function() {
if ($(this).is(":checked")) {
someObj.idsChecked.push($(this).attr("id"));
}
}).attr('checked', false);
$.each(someObj.idsChecked,function(id, val) {
// displays the wanted value, e.g. "mapcat52" if checkbox with this id is checked
console.log(val);
// does not work: inserted value
map.removeLayer(val);
// works: hard coded value of the leaflet.js/input id
map.removeLayer(mapcat52);
});
});
을 선택하면 : https://stackoverflow.com/questions/39186001/ 방법에 가까운 모든 팝업 –