25

I've created a MapBox instance with:

var map = new mapboxgl.Map({
    container : 'map',
    style : 'mapbox://styles/mapbox/streets-v9'
});

I need to clear all markers, and have tried things like map.remove(marker) on each one, and a few other things, but nothing seems to work.

Is there a simple function call to just clear all markers from the map?

EDIT: Different from How to remove all layers and features from map? because I get "eachLayer is not a recognised function" (or similar) in console.

HomerPlata
  • 1,687
  • 5
  • 22
  • 39
  • Possible duplicate of [How to remove all layers and features from map?](https://stackoverflow.com/questions/28646317/how-to-remove-all-layers-and-features-from-map) – xmojmr Sep 12 '17 at 06:29
  • Tried that @xmojmr. It says eachLayer is not a recognised function. – HomerPlata Sep 12 '17 at 07:02

6 Answers6

46

You saw this? https://www.mapbox.com/mapbox-gl-js/api/#marker#remove

Instead of map.remove maybe try marker.remove:

var marker = new mapboxgl.Marker().addTo(map);
marker.remove();
Andi-lo
  • 2,244
  • 21
  • 26
38

If you added multiple markers, and you want to clear all them on your map, you have to loop overs all markers, and delete them one by one, you will have something like this :

if (currentMarkers!==null) {
    for (var i = currentMarkers.length - 1; i >= 0; i--) {
      currentMarkers[i].remove();
    }
}

Consider thar var currentMarkers contains all markers, you can do this with sometning like :

oneMarker= new mapboxgl.Marker(currentMarkerDiv)
    .setLngLat(marker.geometry.coordinates)
    .addTo(mapboxMap);
    currentMarkers.push(oneMarker);

Where var currentMarkers is a global variable :

var currentMarkers=[];

Full example :

// markers saved here
var currentMarkers=[];

// tmp marker
var oneMarker= new mapboxgl.Marker(currentMarkerDiv)
    .setLngLat(marker.geometry.coordinates)
    .addTo(mapboxMap);

// save tmp marker into currentMarkers
currentMarkers.push(oneMarker);


// remove markers 
if (currentMarkers!==null) {
    for (var i = currentMarkers.length - 1; i >= 0; i--) {
      currentMarkers[i].remove();
    }
}
AIT MANSOUR Mohamed
  • 809
  • 1
  • 7
  • 20
1

You can't query for all markers - without a hack - to remove, so instead keep track of them when you add them so you can later iterate and remove.

let mapMarkers = []

// adding
const marker = new mapboxgl.Marker()
                        .setLngLat([0, 0])
                        .addTo(map)
mapMarkers.push(marker)

// removing
mapMarkers.forEach((marker) => marker.remove())
mapMarkers = []
S..
  • 5,511
  • 2
  • 36
  • 43
  • For anyone else using react pure components, store the map and markers via `useState`s then instantiate the map in an effect tracking `[]` and remove/add markers in an effect tracking your data-structure. – S.. Jan 04 '21 at 17:27
0

If you added the markers like this (to be able to add styling and custom images as markers), then you can simply remove the class (I did it through Jquery though).

Adding markers that are in a GeoJson:

GeoJson.features.forEach(function(marker) {
var el = document.createElement('div');
el.className = 'marker';
new mapboxgl.Marker(el).setLngLat(marker.geometry.coordinates).addTo(map);
});

Removing markers:

$( ".marker" ).remove();

Ralph1983
  • 25
  • 3
  • 2
    This is not a good idea because you just remove things from the DOM without cleaning things up in mapbox. They might not be displayed anymore but the markers reference might still exist in memory. – Andi-lo Feb 26 '20 at 12:57
0

For people looking for a simple answer to remove one marker do:

marker.remove()

Here is an example in React:

// Add Marker to map at the specific position
const customMarker = createCustomMarker(newMarkerIcon.current, true) // Separation required for event listeners. Create custom marker adds some css and an icon using custom sources.
const marker = new mapboxgl.Marker({
  element: customMarker
})
  .setLngLat(event.lngLat)
  .addTo(map.current)

// Listens to clicks for removal after placement
customMarker.addEventListener('click', removeMarkerFromClick)
customMarker.addEventListener('touchstart', removeMarkerFromClick)

 function removeMarkerFromClick(event) {
    // Remove marker retrieved from event
    const marker = event.target
    marker.remove()
  }

For many markers look at this answer in this thread.

0

If you are using React and set new position in every click. follow

if (marker.current) {
  marker.current.remove();
}

marker.current = new mapboxgl.Marker()
.setLngLat(ev.lngLat)
.addTo(map.current as any);
Alex Alan Nunes
  • 174
  • 2
  • 9