0

I have to create and export multiple maps that zoom to particular areas of my county. The issue I'm having is that zoom level 12 is too far away, and zoom level 13 is too close. Is there a way to set specific boundaries for a png export of a map through code?

library(leaflet)
library(mapview)

northmap<-leaflet()  %>% addTiles() %>% 
  setView(lng = -90.264027, lat  = 38.803011, zoom = 12) %>% 
  addProviderTiles("Esri")

mapshot(northmap, file = "northmap1.png",
        remove_controls = c("homeButton", "zoomControl"))

1 Answer 1

0

You can set zoomSnap and zoomDelta in the options of the map which will enable smaller zoom increments. Note, though, that the background tiles only exist at integer zoom levels (0, 1, 2, etc.) and as such the background may look a little blurred at intermediate steps.

library(leaflet)
library(mapview)
library(leafem) # for mouseControls to check zoom levels

northmap<-leaflet(options = leafletOptions(zoomSnap = 0.25, zoomDelta = 0.25)) %>% 
  addTiles() %>% 
  setView(lng = -90.264027, lat  = 38.803011, zoom = 12.75) %>% 
  addProviderTiles("Esri") %>%
  leafem::addMouseCoordinates()

northmap

mapshot(northmap, file = "northmap1.png",
        remove_controls = c("homeButton", "zoomControl"))

Note, I added leafem::addMouseCoordinates() to enable checking of the zoom loevels.

4
  • Thanks, that allowed me to zoom in and out at .25 in the viewer, but now my issue is the mapshot is still only exporting at full integers. Do you know what to do about that?
    – Abby
    Commented Oct 1, 2019 at 19:19
  • Are you sure? It seemed to me that setting zoom to 12.5 worked when exporting the map
    – TimSalabim
    Commented Oct 1, 2019 at 19:52
  • Yeah, I tested it at multiple intervals by changing the zoom and comparing the output pngs. The right zoom would be 12.25, but anything less than 12.5 exports at 12 zoom. And 12.5 or higher exports at 13 zoom.
    – Abby
    Commented Oct 2, 2019 at 13:12
  • You're right, I was comparing 12 and 12.75 so thought it worked... I am out of ideas sorry.
    – TimSalabim
    Commented Oct 2, 2019 at 15:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.