jQuery 如何从地图中删除所有图层和要素?

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

How to remove all layers and features from map?

jqueryleafletmapbox

提问by Rohan

I am working on a map and I would like to remove all features from the map on a certain event. The features are in multiple layers which are plotted dynamically.

我正在制作地图,我想从某个事件的地图中删除所有要素。特征位于动态绘制的多个图层中。

Some of the code is:

部分代码如下:

$.getJSON('distributor-companies', function (data) {
                var layers = [];
                $.each(data, function (i, item) {
                    if (item.geojson != '') {
                        layers[i] = L.mapbox.featureLayer().addTo(map);
                        $.getJSON('/geojson/' + item.geojson, function (data) {
                            layers[i].setGeoJSON(data);
                            // Loop over the added layer
                            layers[i].eachLayer(function (layer) {
                                // Add click event
                                layer.on('click', function (e) {
                                    // Do stuff
                                    map.fitBounds(layers[i].getBounds());
                                });
                            });
                        });
                    }
                });
            });

Is there a way to fetch all layers on the map at a certain point in time and remove them.

有没有办法在某个时间点获取地图上的所有图层并删除它们。

回答by iH8

Loop over all the layers added to the map using the eachLayermethod of L.Map, then call the removeLayermethod of L.Mapon each of them:

使用 的eachLayer方法循环添加到地图的所有图层L.Map,然后在每个图层上调用 的removeLayer方法L.Map

map.eachLayer(function (layer) {
    map.removeLayer(layer);
});

References:

参考:

eachLayer: http://leafletjs.com/reference.html#map-eachlayer

eachLayerhttp: //leafletjs.com/reference.html#map-eachlayer

removeLayer: http://leafletjs.com/reference.html#map-removelayer

removeLayerhttp: //leafletjs.com/reference.html#map-removelayer

Please note that this wil remove ALL the layers from your map. That means also any tilelayers etc. I think in your case it would be best if you do not add all your featureLayers to the map instance, but create a group for them:

请注意,这将从您的地图中删除所有图层。这也意味着任何 tilelayers 等。我认为在您的情况下,最好不要将所有 featureLayers 添加到地图实例中,而是为它们创建一个组:

// Create group for your layers and add it to the map
var layerGroup = L.layerGroup().addTo(map);

$.getJSON('distributor-companies', function (data) {

    $.each(data, function (i, item) {
        if (item.geojson != '') {
            // Add the new featureLayer to the layerGroup
            var featureLayer = L.mapbox.featureLayer().addTo(layerGroup);
            $.getJSON('/geojson/' + item.geojson, function (data) {
                featureLayer.setGeoJSON(data);
                featureLayer.eachLayer(function (layer) {
                    layer.on('click', function (e) {
                        map.fitBounds(featureLayer.getBounds());
                    });
                });
            });
        }
    });
});

Now you can call the clearLayersmethod of L.LayerGroupwhich will clear of the current layers in the group:

现在,你可以调用clearLayers的方法L.LayerGroup,这将清除该组在当前层:

layerGroup.clearLayers();

Reference:

参考:

L.LayerGroup: http://leafletjs.com/reference.html#layergroup

L.LayerGrouphttp: //leafletjs.com/reference.html#layergroup

clearLayers: http://leafletjs.com/reference.html#layergroup-clearlayers

clearLayershttp: //leafletjs.com/reference.html#layergroup-clearlayers

回答by sybb

You can use the following truthy check to see if it's a valid geoJSON object:

您可以使用以下真实检查来查看它是否是有效的 geoJSON 对象:

map.eachLayer(function(layer) {
  if (!!layer.toGeoJSON) {
    map.removeLayer(layer);
  }
});

回答by Viraj Amarasinghe

In the mabox-gl draw library, There is a function to do this.

在 mabox-gl 绘图库中,有一个函数可以做到这一点。

draw.deleteAll().getAll();

This deletes all the features and layers on the map.

这将删除地图上的所有要素和图层。