leaflet remove marker

To remove a marker from a LeafletJS map, you can use the marker.remove() function. For example, the following code removes a marker from the map:

1
2
3
4
5
var marker = L.marker([51.505, -0.09]);
map.addLayer(marker);

// Remove the marker from the map.
marker.remove();

You can also remove a marker from a LeafletJS map by removing the layer that contains the marker. For example, the following code removes a marker from the map by removing the layer that contains the marker:

1
2
3
4
5
var markerLayer = L.marker([51.505, -0.09]);
map.addLayer(markerLayer);

// Remove the layer from the map.
map.removeLayer(markerLayer);

Which method you use to remove a marker from the map depends on how you added the marker to the map. If you added the marker directly to the map, you should use the marker.remove() function. If you added the marker to a layer, you should remove the layer from the map.

You can learn more about removing markers from LeafletJS maps from the LeafletJS documentation.