derdida
derdida

Reputation: 14904

MapBox GeoJSON clear all markers?

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

Answers (2)

derdida
derdida

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

Taylor Daughtry
Taylor Daughtry

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

Related Questions