Javascript 清除传单地图中的所有折线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14585688/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:17:41  来源:igfitidea点击:

Clear all polylines from leaflet map

javascriptleaflet

提问by Vince Lowe

I am struggling to clear all polylines from my map, i only clear the newest.

我正在努力清除地图上的所有折线,我只清除最新的。

var polylines;

// add map polylines
function addPolyline(polyArray, colour) {
    polylines = L.polyline(polyArray, {color: colour});
    polylines.addTo(map);
}

// clear polylines   
function clearPolylines() {
    map.removeLayer(polylines);
}

where addPolylines is called multiple times and clear Polylines is called once. How can i clear all polylines on the map?

其中 addPolylines 被多次调用, clear Polylines 被调用一次。如何清除地图上的所有折线?

回答by flup

You'll have to remember them all or cheat a bit and peek into map._layersto find them.

您必须全部记住它们或作弊并偷看map._layers以找到它们。

EDIT adding sample code by @Ben:

编辑通过@Ben 添加示例代码:

function clearMap() {
    for(i in m._layers) {
        if(m._layers[i]._path != undefined) {
            try {
                m.removeLayer(m._layers[i]);
            }
            catch(e) {
                console.log("problem with " + e + m._layers[i]);
            }
        }
    }
}

回答by Mike K.

The following will remove both polygons and markers but keep the image tiles in the background:

以下将删除多边形和标记,但将图像图块保留在背景中:

for (i in map._layers) {
    if (map._layers[i].options.format == undefined) {
        try {
            map.removeLayer(map._layers[i]);
        } catch (e) {
            console.log("problem with " + e + map._layers[i]);
        }
    }
}