Get startedGet started for free

Cleaning up the Base Map

If you are storing leaflet maps in objects, there will come a time when you need to remove markers or reset the view. You can accomplish these tasks with the following functions.

  • clearMarkers()- Remove one or more features from a map
  • clearBounds()- Clear bounds and automatically determine bounds based on map elements

To remove the markers and to reset the bounds of our m map we would:

m <- m  %>% 
        addMarkers(lng = dc_hq$lon, lat = dc_hq$lat) %>% 
        setView(lat = 50.9, lng = 4.7, zoom = 5)

m  %>% 
    clearMarkers() %>% 
    clearBounds()

The leaflet map of DataCamp's headquarters has been printed for you.

This exercise is part of the course

Interactive Maps with leaflet in R

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Remove markers, reset bounds, and store the updated map in the m object
map_clear <- map  %>%
        clear___()  %>% 
        clear___()
Edit and Run Code