Reputation: 14904
I would like to update my map with "new" markers. So reset all visible markers first. Ill read my markers by geoJSON by Ajax with:
$.ajax({
url: 'dashboard/geoJSON',
dataType: 'json',
async: false,
type: 'GET',
success: function(geojson) {
var locations = L.mapbox.featureLayer().addTo(map);
locations.setGeoJSON(geojson);
// reset?
}
});
Ill tried with:
L.mapbox.featureLayer().clearLayers();
To reset before adding all new markers, but it is not working. Any ideas?
Upvotes: 0
Views: 2563
Reputation: 14904
Ill found a solution:
success: function(geojson) {
markers.forEach(function(entry) {
map.removeLayer(entry);
});
locations = L.mapbox.featureLayer().addTo(map);
locations.on('layeradd', function (e) {
var marker = e.layer;
markers.push(marker);
});
locations.setGeoJSON(geojson);
So ill use the 'layeradd' method to manually push my array, and then remove all before setting new ones.
Upvotes: 1
Reputation: 249
You should clear the layers from the map
object, not featureLayers
, where the layer
variable is the layer with all your markers:
map.removeLayer(layer);
Think of the global L
object as a 'generator' of sorts. You can chain off it to create maps, layers, markers, and more. You'll attach these to a particular map by chaining off the map.
Upvotes: 0