سؤال

I'm trying to remove marker from my map before I add a different one but the suggested method, although throwing no error, doesn't remove my marker.

                $scope.geo.push({
                    type: 'Feature',
                    geometry: {
                        type: 'Point',
                        coordinates: [$scope.gig.lng, $scope.gig.lat]
                    },
                    properties: {
                        title: $scope.gig.venue,
                        description: $scope.gig.address + ' ' + $scope.gig.postcode,
                        'marker-size': 'medium',
                        'marker-color': '#676767'
                    }
                });

                /* show on map */

                var markerLayer = L.mapbox.markerLayer().setGeoJSON({
                    type: 'FeatureCollection',
                    features: $scope.geo
                }).addTo(map);

                map.setZoom(13);

                map.panTo($scope.geo[0].geometry.coordinates.reverse());
                markerLayer.eachLayer(function(m) {
                });

According to the documentation I should be able to then call the following to clear all markers but it does nothing.

L.mapbox.markerLayer().clearLayers();

Am I doing something wrong? If not is there a nuclear way of resetting the map?

هل كانت مفيدة؟

المحلول

L.mapbox.markerLayer().clearLayers();

L.mapbox.markerLayer() is a function that creates a new marker layer: this call is creating a new marker layer, and clearing the markers in it.

In your code, you have the lines

           var markerLayer = L.mapbox.markerLayer().setGeoJSON({
                type: 'FeatureCollection',
                features: $scope.geo
           }).addTo(map);

You are creating a new marker layer with the L.mapbox.markerLayer() and naming it with the variable markerLayer. So, to clear the markers in this layer, you would call:

markerLayer.clearLayers();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top