MulaiMulai sekarang secara gratis

Change up the Base

Similar to how we added overlay groups (i.e., college sectors), we can allow our users to toggle between different base maps using the baseGroups argument to the addLayersControl() function.

First we will need to add multiple basemaps and specify the name of a group for each.

leaflet() %>% 
  addTiles(group = "OSM") %>% 
  addProviderTiles("CartoDB", group = "CartoDB") 

Try running the above code in the console. You should see a leaflet map in the viewer with the CartoDB basemap. This is because we added the CartoDB base map after the default OpenStreetMap tile. If we add addLayerControl(), our users will be able to toggle between the two base maps (you can include many base maps, but only one can be selected at a time).

Latihan ini adalah bagian dari kursus

Interactive Maps with leaflet in R

Lihat Kursus

Petunjuk latihan

  • Add the OSM, CartoDB and Esri tiles.
  • Use the addLayersControl() function to allow users to toggle between the three base maps.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

leaflet() %>% 
  # Add the OSM, CartoDB and Esri tiles
  addTiles(group = "OSM") %>% 
  addProviderTiles(___, group = "CartoDB") %>% 
  addProviderTiles("Esri", group = "Esri") %>% 
  # Use addLayersControl to allow users to toggle between basemaps
  addLayersControl(baseGroups = c(___, ___, ___))
Edit dan Jalankan Kode