javascript 传单清除geojson层(折线)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12712557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Leaflet clear geojson layer (polyline)
提问by Joel James
I am a creating a polyline using a geojson. The format of my geojson is as below:
我正在使用 geojson 创建多段线。我的 geojson 格式如下:
var myLines = [{
"type": "LineString",
"coordinates": [[-75, 21.9], [-75.4, 22.7], [-76.5, 23.4]]
}, {
"type": "LineString",
"coordinates": [[-105, 40], [-110, 45], [-115, 55]]
}];
L.geoJson(myLines).addTo(map);
I have a clear polyline function as below:
我有一个清晰的折线函数,如下所示:
function clear_polyline(){
$.each(myLines, function(ind,poly){
map.removeLayer(poly);
});
}
This function does not clear the layer nor does it throw any error. How do I clear a polyline in leaflet?
此函数不会清除图层,也不会引发任何错误。如何清除传单中的折线?
回答by InPursuit
You need to remove the Leaflet layer from the map, not the GeoJSON object that the layer was created from. L.GeoJSON is a FeatureLayer that will contain all of the items in "myLines" so you should do something like this to remove it:
您需要从地图中删除 Leaflet 图层,而不是创建图层的 GeoJSON 对象。L.GeoJSON 是一个 FeatureLayer,它将包含“myLines”中的所有项目,因此您应该执行以下操作以将其删除:
var linesFeatureLayer = L.geoJson(myLines);
linesFeatureLayer.addTo(map);
function clear_polyline() {
map.removeLayer( linesFeatureLayer );
}
回答by Small Jheng
var linesFeatureLayer = L.geoJson(myLines);
linesFeatureLayer.clearlayer()