Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Leaflet Offline Map Tiles Not Loading

Tags:

r

leaflet

I need help trying to figure out why my leaflet map using locally saved map tiles isn't working correctly. I'm trying to recreate the example from here to create a leaflet map based on map tiles saved locally. However, when I create it the background map tiles don't load.

The code I have is basically straight from the example, but updated for my directory, and updated to start my local server. I'm not sure if I'm trying to start the server wrong. I'm also looking here for instructions on how to start the local server using servr.

library(RgoogleMaps)
for (zoom in 10:16)
GetMapTiles("Washington Square Park;NY", zoom = zoom,
          nTiles = round(c(20,20)/(17-zoom)))

library(leaflet)
setwd("C:/Users/OTAD USER/Documents")
system("Rscript -e 'servr::httd()' -p8000")
m = leaflet() %>% 
    addTiles( urlTemplate = "http:/localhost:8000/mapTiles/OSM/{z}_{x}_{y}.png")
m = m %>% setView(-73.99733, 40.73082 , zoom = 13)
m = m %>% addMarkers(-73.99733, 40.73082 )
m
like image 612
Jake Avatar asked Feb 05 '23 21:02

Jake


1 Answers

You were almost there. You can run the server in daemon mode, with servr::httd(port = 8000, daemon = TRUE):

# Set the working folder
setwd("C:/Users/OTAD USER/Documents")

# Load the tiles in working_folder/mapTiles/OSM/
library(RgoogleMaps)
for (zoom in 10:16)
  GetMapTiles("Washington Square Park;NY", zoom = zoom,
              nTiles = round(c(20,20)/(17-zoom)))

# Start serving working folder on port 8000 in demon mode
deamon_id <- servr::httd(port = 8000, daemon = TRUE)

# Plot with leaflet
library(leaflet)
m = leaflet() %>% 
  addTiles( urlTemplate = "http:/localhost:8000/mapTiles/OSM/{z}_{x}_{y}.png")
m = m %>% leaflet::setView(-73.99733, 40.73082 , zoom = 16)
m = m %>% leaflet::addMarkers(-73.99733, 40.73082 )
m

# Stop serving
servr::daemon_stop(deamon_id)
like image 140
HubertL Avatar answered Feb 20 '23 15:02

HubertL