Remove all markers from mapbox map or reinitialize map

Mahmad Khoja picture Mahmad Khoja · Apr 9, 2014 · Viewed 11.4k times · Source

I am using below code to add markers to mapbox map.

var marker = L.marker([latitude, longitude],
                        {
                            icon: L.divIcon({
                                html: 'myhtml',
                                iconSize: [40, 40]
                            })
                        });
marker.addto(map);

I have added markers using for loop using above code. Now I want to remove marker to refresh the map. Is there any was I can remove all markers and polylines from map. Or if there is any way I can reload the mapbox map.

Answer

geografa picture geografa · Apr 10, 2014

You can use map.removeLayer(marker); to remove the marker (an ILayer object).

Also, addto(map) should be addTo ;)