我一直在尝试调整这个示例来过滤基于复选框ID的传单层,它的工作方式是填充启用的对象,并将相应的功能过滤并添加到地图中。
但是,当我取消选中该框对象时,该框对象将变为空,但该功能将保留在地图上,因为没有什么可判断是否要删除。
在这里我需要做些什么来删除这个特性?
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)
};发布于 2016-08-09 08:43:10
每当脚本执行update()函数时,它都会创建一个新的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)
};https://stackoverflow.com/questions/38845292
复制相似问题