No se pueden eliminar capas con clústeres en leaflet.js
Frecuentes
Visto 4,817 veces
3
Estoy usando el leaflet cluster plugin to map out a bunch of crashes. When you load up the page, I map out all crashes (stored in a GeoJSON file). I also have buttons that load up a different geoJSON file for each year.
The problem I am running into is that removeLayer() isn't actually removing the layers. So when I remap a new GeoJSON file, it's just stacking all the crashes on top of each other.
These are my two functions that fire when you click a button. marcadores var is defined in the global namespace, seen below (maybe this is part of the problem)?
var markers = L.markerClusterGroup({spiderfyDistanceMultiplier: 1.3});
//Lay new data on map
function buildMarkers(yearData){
var crashLayer = new L.geoJson(yearData, {
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.year);
}
});
markers.addLayer(crashLayer);
map.addLayer(markers);
}
//Remove ALL data on map
function removeClusters(){
map.removeLayer(markers)
}
//AN example of a button
$("#twelve").click(function(e){
e.preventDefault();
$('a').removeClass('selected');
$(this).addClass('selected');
removeClusters();
buildMarkers(twelveCrashes);
});
2 Respuestas
15
Lo acabo de descubrir.
To clear all the data from the markers variable, I used clearLayers(). So in my buildMarkers() function I just add this to the first line in the function
markers.clearLayers();
and that clears away all the data.
Respondido el 09 de Septiembre de 13 a las 21:09
0
we can remove clusters layers by using disableClusteringAtZoom
need to specified that at which label we can remove the Clustering
var markers = L.markerClusterGroup({disableClusteringAtZoom: 17, spiderfyOnMaxZoom: false, showCoverageOnHover: true, zoomToBoundsOnClick: true });
respondido 13 mar '18, 07:03
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript jquery leaflet or haz tu propia pregunta.