Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all markers from mapbox map or reinitialize map

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.

like image 931
Mahmad Khoja Avatar asked Apr 09 '14 10:04

Mahmad Khoja


People also ask

How do I hide or delete markers on the map?

Click the buttons to hide or delete all of those markers. Read the documentation. // In the following example, markers appear when the user clicks on the map. // The markers are stored in an array. // The user can then click an option to hide, show or delete the markers.

How to turn layers on and off in Mapbox?

Finally, take a look at how to toggle layers in Mapbox.js. You can help users sift through your markers by adding filters to let them turn layers on and off. In this example, the script automatically creates a toggle option for each transit line and will only add a new layer to the toggle list if its line is declared in the GeoJSON.

How to add markers to a Mapbox map?

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);

Is Mapbox JS still in active development?

Mapbox.js is no longer in active development. To learn more about our newer mapping tools see Add custom markers in Mapbox GL JS. In this guide, we’ll show how to add markers, customize them, and make them interactive with Mapbox.js.


1 Answers

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

Also, addto(map) should be addTo ;)

like image 61
geografa Avatar answered Sep 16 '22 16:09

geografa