在客户端上我从postgres数据库中获取了一堆多边形,然后我将它们作为叠加层添加到图层控件中。目前通过菜单点击发生这种情况,并且每次在控件中添加另一个“多边形”复选框。我需要首先清除任何旧的叠加层并使用一组新的“多边形”叠加层进行刷新。如何在添加新叠加层之前以编程方式删除它?
var controlLayers = L.control.layers(baseMaps).addTo(map);
$.getJSON('/db/getPolys', function (geojsonFeatures) {
console.log('returned polys...',geojsonFeatures.features);
if(geojsonFeatures.features == null){
alert('No polygons where found');
}else{
geojsonLayer = L.geoJson(geojsonFeatures,{
onEachFeature: drawStep1Polys
});
// need to remove before adding anything
// if (geojsonLayer exists){
// controlLayers.removeLayer(geojsonLayer);
// }
controlLayers.addOverlay(geojsonLayer, 'Polygons');
}
});
答案 0 :(得分:2)
答案 1 :(得分:0)
谢谢,顺便说一下,这是最终的代码:
var group;
$.getJSON('/FIMS/getPolys', function (geojsonFeatures) {
console.log('returned polys...',geojsonFeatures.features);
if(geojsonFeatures.features == null){
var message = 'No polygons where found';
alert(message);
}else{
if (group) {
controlLayers.removeLayer(group);
}
group = L.geoJson(geojsonFeatures, {
onEachFeature: drawStep1Polys
}).addTo(map);
controlLayers.addOverlay(group, 'Polygons');
map.fitBounds(step1Bounds);
}
});