2016-08-09 2 views
0

나는이 example을 적용하여 체크 박스 ID를 기반으로 리플릿 레이어를 필터링하려고했습니다. 활성화 된 객체가 채워지고 해당 지형이 필터링되어지도에 추가되는 일종의 작품입니다.필터 전단지 Geojson 객체는 체크 박스 상태를 기반으로합니다.

그러나 확인란을 선택 취소하면 객체가 비게되지만 지울 지 여부는 아무 것도 없으므로 지형지 물이지도에 남아 있습니다.

기능을 제거하려면 무엇을해야합니까? 스크립트가 귀하의 update() 기능을 실행 할 때마다

function update() { 
     var enabled = {}; 
     // Run through each checkbox and record whether it is checked. If it is, 
     // add it to the object of types to display, otherwise do not. 
     for (var i = 0; i < checkboxes.length; i++) { 
      if (checkboxes[i].checked) enabled[checkboxes[i].id] = true; 
     } 
     L.geoJson(parishesData, { 
     filter: function(feature, layer) { 
      return (feature.properties.name in enabled); 
      } 
     }).addTo(map); 
     console.log(enabled) 
     }; 

답변

2

, 그것은 새로운 L.geoJson 그룹을 만들고지도에 추가합니다.

지도에 새 그룹을 추가하기 전에 해당 그룹에 대한 참조를 유지하고지도에서 삭제해야합니다.

은 아마 같은 :

var group; 

function update() { 
    var enabled = {}; 
    // Run through each checkbox and record whether it is checked. If it is, 
    // add it to the object of types to display, otherwise do not. 
    for (var i = 0; i < checkboxes.length; i++) { 
    if (checkboxes[i].checked) enabled[checkboxes[i].id] = true; 
    } 
    if (group) { 
    map.removeLayer(group); 
    } 
    group = L.geoJson(parishesData, { 
    filter: function(feature, layer) { 
     return (feature.properties.name in enabled); 
    } 
    }).addTo(map); 
    console.log(enabled) 
}; 
+0

큰 덕분 그! 비슷한 것을 시도했지만 매번 L.geoJson 그룹을 제거하지 않았습니다. –

관련 문제