Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

jquery - How to remove all layers and features from map?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

References:

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

removeLayer: http://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:

// 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 clearLayers method of L.LayerGroup which will clear of the current layers in the group:

layerGroup.clearLayers();

Reference:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...