Search code examples
rmapboxopenstreetmap

How to get mapbox style openmap with API key?


I'm trying to make a map in R using OpenStreetMap library.

I had no problem using types such as osm, stamen-toner, stamen-terrain and stamen-watercolor, for instance:

map <- openproj(openmap(c(63.47,10.32),c(63.37,10.54),type='osm'),proj=CRS('+init=epsg:32632'))
par(mar=c(0,0,0,0))
plot(map)

works just fine:

map

I'll add the points over that layer. However, my client wants a map-box type, but when I run

map <- openproj(openmap(c(63.47,10.32),c(63.37,10.54),type='mapbox'),proj=CRS('+init=epsg:32632'))

I get the following error:

failed loading 12/2165/1106 Server returned HTTP response code: 401 for URL: http://api.tiles.mapbox.com/v4/examples.map-zr0njcqy/12/2165/1106.png?access_token=pk.eyJ1IjoidGhlZmVsbCIsImEiOiJjaXN1anNwODEwMWlrMnRvZHBhamRrZjlqIn0.Gf8qLSpZ6yo5yfQhEutFfQ
java.lang.NullPointerException
    at edu.cens.spatial.RTileController.getTileValues(RTileController.java:109)
    at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at RJavaTools.invokeMethod(RJavaTools.java:386)
Error in osmtile(x%%nX, y, zoom, type) : 
  could not obtain tile: 2165 1106 12

I know I need an API key. I've created one at MapBox site. But now, how do I get a map of map-box type? The examples given are like

apiKey <- paste0('?access_token=','{my key}')
baseUrl <- 'https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v9/tiles/256/{z}/{x}/{y}'
map <- openproj(openmap(c(63.47,10.32),c(63.37,10.54),type=paste0(baseUrl,apiKey)),proj=CRS('+init=epsg:32632'))

I tried replacing satellite-streets-v9 with mapbox or simply deleting this directory, but it doesn't work. I don't know how to add my API key to the original command openmap(c(63.47,10.32),c(63.37,10.54),type='mapbox'). I couldn't find anything useful on mapbox's website, either.


Solution

  • I had to enter Mapbox Studio, create a default style (similar to the mapbox style I desired), to see on the "Layer overview" window that their default theme is called "Mapbox Streets v8". From there, I guessed that I should use

    https://api.mapbox.com/styles/v1/mapbox/streets-v8/tiles/256/{z}/{x}/{y}
    

    as my baseUrl. And it worked!