Leaflet: To remove or clear all active layers without passing specific layer.

Created on 18 Feb 2015  路  2Comments  路  Source: Leaflet/Leaflet

I did not find any way to clear or remove all active layers. I think there should be a function to remove all active layers without passing the specific layer. what do you think?

I found 2 major functions for this purpose.
map.removeLayer(Markers);
Markers.clearLayers();

But I am having a problem while using this function... actually, it works just once which means remove only one layer of markers or maybe i am doing something wrong.

var map = new L.Map('map');

function getMarker(){
var heatMap = "";
if(Markers) {
map.removeLayer(Membersx);
map.removeLayer(map._layers);
}
$.ajax({
type: "post",
url: "app_lib->FOLDER['admin'].$this->router->class ?>/buscador",
dataType: "json",
data: {layers:}
success: function(result){
$.each(result, function(index, layer) {
var Markers = L.geoJson(layer.data ,{
pointToLayer: function(feature, latlng) {
if(feature.geometry.type == 'Point'){
return L.marker(latlng, {icon: L.ExtraMarkers.icon({ icon: feature.marker_icon, markerColor: feature.marker_color, shape: feature.marker_type, prefix: 'fa' }) })
}
},
onEachFeature: onEachFeature
});
map.addLayer(Markers);
});
}

May you please help me on this issue...?

Thanks in advance.

Most helpful comment

There is more than sufficient functionality for removing layers from the map or removing from / clearing other grouped layers. L.Map has the removeLayer method which would work fine in combination with the eachLayer function;

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

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

If you need that functionality a lot, you can include it into L.Map easily:

L.Map.include({
  'clearLayers': function () {
    this.eachLayer(function (layer) {
      this.removeLayer(layer);
    }, this);
  }
});

The reason (i'm speculating here) this isn't included in L.Map is that very few people will ever need it. Most implementations will use one of the grouped layer types like L.LayerGroup, L.FeatureGroup or L.GeoJSON to create collections of layers. Those layers do have the clearLayers method included.

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

All the functionality you need is already there, you just need to implement it correctly. If you're having problems or need help with your implementation then posting to this issuetracker isn't the right thing to do. You'll get feedback and help much sooner if you use the proper channels for that like in the Leaflet's Google group or posting a question on Stackoverflow with the leaflet tag.

All 2 comments

There is more than sufficient functionality for removing layers from the map or removing from / clearing other grouped layers. L.Map has the removeLayer method which would work fine in combination with the eachLayer function;

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

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

If you need that functionality a lot, you can include it into L.Map easily:

L.Map.include({
  'clearLayers': function () {
    this.eachLayer(function (layer) {
      this.removeLayer(layer);
    }, this);
  }
});

The reason (i'm speculating here) this isn't included in L.Map is that very few people will ever need it. Most implementations will use one of the grouped layer types like L.LayerGroup, L.FeatureGroup or L.GeoJSON to create collections of layers. Those layers do have the clearLayers method included.

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

All the functionality you need is already there, you just need to implement it correctly. If you're having problems or need help with your implementation then posting to this issuetracker isn't the right thing to do. You'll get feedback and help much sooner if you use the proper channels for that like in the Leaflet's Google group or posting a question on Stackoverflow with the leaflet tag.

^^ :+1:
You could also just remember your Markers layer and remove that.

Was this page helpful?
0 / 5 - 0 ratings