Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To remove or clear all active layers without passing specific layer. #3238

Closed
princeshahnawaz2012 opened this issue Feb 18, 2015 · 4 comments

Comments

@princeshahnawaz2012
Copy link

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.

@princeshahnawaz2012 princeshahnawaz2012 changed the title To remove or clean all active layers. To remove or clear all active layers without passing specific layer. Feb 18, 2015
@iH8
Copy link
Contributor

iH8 commented Mar 3, 2015

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.

@danzel
Copy link
Member

danzel commented Mar 9, 2015

^^ 👍
You could also just remember your Markers layer and remove that.

@saptisunil
Copy link

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.

here once when i m doing its saying like map is not defined. Actually i have a mapContainer in the render section below.

 <MapContainer className="map" scrollWheelZoom={false} ref={this.mapRef} id="map" name="map">
                  <FeatureGroup>
                    <EditControl position="topleft" onCreated={this.created} onEdited={this.edited} onDeleted={this.deleted} draw={{circle:false, marker:false, polygon:false, polyline:false}} />
                  </FeatureGroup >
                  <TileLayer
                    attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                  />
 </MapContainer>

Also if i give this

 var map = L.map('map', {
    center: [51.505, -0.09],
    zoom: 13
});

then it shows already mapContainer exists error. So i dont know how to do?
i want to clear all the layers the users have drawn once while submitting the form. So how will i do ? Thanks in advance for the help.

@Falke-Design
Copy link
Member

Please aks for help on StackOverflow, this is a old Issue where nobody is looking into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants