Basemap providers

This example shows how to programmatically add tile providers, and add them to a layers control.

library(leaflet)

# Use all providers that begin with "Esri"
esri <- grep("^Esri", providers, value = TRUE)

m <- leaflet() %>%
  setView(0,0,1) %>%
  addLayersControl(
    baseGroups = names(esri),
    options = layersControlOptions(collapsed = TRUE))

for (provider in esri) {
  m <- m %>% addProviderTiles(provider, group = provider)
}

m

Provider with options

This example shows how to pass an options argument to addProviderTiles to add specific options to a provider.

mapbox.tileIds <- list(Satellite='mapbox.satellite',
                       Terrian='mapbox.mapbox-terrain-v2')

m <- leaflet() %>%
  setView(0,0,1) %>%
  addLayersControl(
    baseGroups = names(mapbox.tileIds),
    options = layersControlOptions(collapsed = FALSE))

for (tileId in names(mapbox.tileIds)) {
  m <- m %>%
    addProviderTiles(providers$MapBox, group = tileId,
      options = providerTileOptions(
        detectRetina = TRUE,
        # id and accessToken are Mapbox specific options
        id = mapbox.tileIds[[tileId]] ,
        accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN')
      ))
}

m

The "leaflet" R package is copyright © 2014-2016 RStudio, Inc.
The Leaflet JavaScript library is © 2010–2016 Vladimir Agafonkin, 2010–2011 CloudMade.
Maps © OpenStreetMap contributors unless otherwise noted.

Fork me on GitHub