|
| 1 | +--- |
| 2 | +pagetitle: Leaflet for R - Using Basemaps |
| 3 | +--- |
| 4 | + |
| 5 | +## Using Basemaps |
| 6 | + |
| 7 | +Leaflet supports basemaps using [map tiles](https://www.mapbox.com/guides/how-web-maps-work/), popularized by Google Maps and now used by nearly all interactive web maps. |
| 8 | + |
| 9 | +### Default (OpenStreetMap) Tiles |
| 10 | + |
| 11 | +The easiest way to add tiles is by calling `addTiles()` with no arguments; by default, [OpenStreetMap](https://www.openstreetmap.org/) tiles are used. |
| 12 | + |
| 13 | +```{r fig.height=2.5} |
| 14 | +m <- leaflet() %>% setView(lng = -71.0589, lat = 42.3601, zoom = 12) |
| 15 | +m %>% addTiles() |
| 16 | +``` |
| 17 | + |
| 18 | +### Third-Party Tiles |
| 19 | + |
| 20 | +Alternatively, many popular free third-party basemaps can be added using the `addProviderTiles()` function, which is implemented using the [leaflet-providers plugin](https://github.com/leaflet-extras/leaflet-providers). See [here](http://leaflet-extras.github.io/leaflet-providers/preview/index.html) for the complete set. |
| 21 | + |
| 22 | +As a convenience, leaflet also provides a named list of all the third-party tile providers that are supported by the plugin. This enables you to use auto-completion feature of your favorite R IDE (like RStudio) and not have to remember or look up supported tile providers; just type `providers$` and choose from one of the options. You can also use `names(providers)` to view all of the options. |
| 23 | + |
| 24 | +```{r fig.height=1.25} |
| 25 | +m %>% addProviderTiles(providers$Stamen.Toner) |
| 26 | +m %>% addProviderTiles(providers$CartoDB.Positron) |
| 27 | +m %>% addProviderTiles(providers$Esri.NatGeoWorldMap) |
| 28 | +``` |
| 29 | + |
| 30 | +Note that some tile set providers require you to register; see the [project page](https://github.com/leaflet-extras/leaflet-providers) for more information. You can pass access tokens/keys, and other options, to the tile provider by populating the `options` argument with the `providerTileOptions()` function. |
| 31 | + |
| 32 | +### Custom Tile URL Template |
| 33 | + |
| 34 | +If you happen to have a custom map tile URL template to use, you can provide it as an argument to `addTiles()`. |
| 35 | + |
| 36 | +### WMS Tiles |
| 37 | + |
| 38 | +You can use `addWMSTiles()` to add WMS (Web Map Service) tiles. The map below shows the Base Reflectivity (a measure of the intensity of precipitation occurring) using the WMS from the [Iowa Environmental Mesonet ](http://mesonet.agron.iastate.edu): |
| 39 | + |
| 40 | +```{r fig.height=2.5} |
| 41 | +leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>% |
| 42 | + addWMSTiles( |
| 43 | + "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi", |
| 44 | + layers = "nexrad-n0r-900913", |
| 45 | + options = WMSTileOptions(format = "image/png", transparent = TRUE), |
| 46 | + attribution = "Weather data © 2012 IEM Nexrad" |
| 47 | + ) |
| 48 | +``` |
| 49 | + |
| 50 | +### Combining Tile Layers |
| 51 | + |
| 52 | +You aren't restricted to using a single basemap on a map; you can stack them by adding multiple tile layers. This generally only makes sense if the front tiles consist of semi transparent tiles, or have an adjusted opacity via the `options` argument. |
| 53 | + |
| 54 | +```{r fig.height=1.75} |
| 55 | +m %>% addProviderTiles(providers$MtbMap) %>% |
| 56 | + addProviderTiles(providers$Stamen.TonerLines, |
| 57 | + options = providerTileOptions(opacity = 0.35)) %>% |
| 58 | + addProviderTiles(providers$Stamen.TonerLabels) |
| 59 | +``` |
0 commit comments