Leaflet / Leaflet.markercluster

Marker Clustering plugin for Leaflet
MIT License
3.98k stars 1k forks source link

clearLayers method does not remove the layers from the map #1034

Open seerj30 opened 3 years ago

seerj30 commented 3 years ago

I'm using "leaflet.markercluster": "^1.5.0" into an Angular app. The marker cluster is created as follow:

markerCluster = new MarkerClusterGroup({
    spiderfyOnMaxZoom: false,
    showCoverageOnHover: false,
    disableClusteringAtZoom: this.maxZoom,
});

then is added to the map this.map.addLayer(this.markerCluster);. Adding markers to the marker cluster works like a charm (this.markerCluster.addLayers(this.markerLayers);) but the problem appears when I'm trying to delete them because this.markerCluster.clearLayers(); does not remove the layers from the map and I have to do it manually:

this.markerLayers.forEach((x) => {
    this.map.removeLayer(x);
});

Isn't expected from clearLayers method to remove the layers also from the map?

IvanSanchez commented 3 years ago

Can you publish a fiddle/codepen/plunkr that displays this behaviour?

And I mean "without angular", because I've already seen https://stackoverflow.com/questions/69271376/marker-cluster-clearlayers-method-does-not-remove-the-layers-from-the-map

seerj30 commented 3 years ago

It will take a couple of hours to extract it. But I think I found something similar here: https://stackoverflow.com/questions/42862370/refresh-marker-clusters-after-geojson-layer-has-changed https://github.com/Leaflet/Leaflet.markercluster/issues/647#issuecomment-200188598 It's like the marker cluster group is decoupled from the map.