2

i'm use to

Reference https://docs.mapbox.com/mapbox-gl-js/example/drag-a-marker/

var marker = new mapboxgl.Marker({
draggable: true
})
.setLngLat([0, 0])
.addTo(map);

how to remove marker?

 marker = new mapboxgl.Marker({
                draggable: true,                
            })
                .setLngLat([0, 0])
                .addTo(map);
marker.remove() /// fail

i want remove marker

Mike
  • 16,042
  • 2
  • 14
  • 30
Magic
  • 41
  • 1
  • 4

2 Answers2

0
marker.addTo(map).setLngLat([0, 0]).remove(); 
william
  • 606
  • 3
  • 14
  • 29
0

I found this answer here and it worked for me (there are other answers there too, but this is the only one that worked for me). If you pass an html element with a given className (in this case "marker") when you create new markers, you can remove them all with jquery like this.

import $ from 'jquery';

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

$( ".marker" ).remove();
Alex
  • 2,154
  • 3
  • 26
  • 49