如何从地图上删除所有图层和要素? [英] How to remove all layers and features from map?

查看:1562
本文介绍了如何从地图上删除所有图层和要素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理地图,因此我想在特定事件中从地图中删除所有功能.这些要素位于动态绘制的多层中.

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.

其中一些代码是:

$.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.

推荐答案

使用L.MapeachLayer方法遍历添加到地图的所有图层,然后在每个图层上调用L.MapremoveLayer方法其中:

Loop over all the layers added to the map using the eachLayer method of L.Map, then call the removeLayer method of L.Map on each of them:

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

参考:

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

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

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

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:

// 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());
                    });
                });
            });
        }
    });
});

现在,您可以调用L.LayerGroupclearLayers方法,该方法将清除组中的当前图层:

Now you can call the clearLayers method of L.LayerGroup which will clear of the current layers in the group:

layerGroup.clearLayers();

参考:

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

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

这篇关于如何从地图上删除所有图层和要素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆