I'm building a map tool in R using leaflet, and I would like to restrict the zoom to a certain area, but the setMaxBounds
function doesn't seem to have any effect.
library(dplyr)
library(leaflet)
library(tigris)
ohio_map <- leaflet(counties('OH', cb = TRUE)) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(weight = .3,
color = "#229922",
layerId = ~NAME) %>%
setMaxBounds(lng1 = -84.800,
lat1 = 42.000,
lng2 = -80.500,
lat2 = 38.400)
ohio_map
This shows the right area of the map, but doesn't prevent zooming out.
It would be even better to remove the zoom controls altogether, so that I could replace the navigation with something more suitable to the application at hand. I found a zoomControl option, but haven't been able to figure out where to put that in R to get it to work.
EDIT: As pointed out by @Symbolix, setMaxBounds really is something different than what I'm looking for. I really just want to disable zooming altogether, and remove the controls. The zoomControl option described in the leaflet JavaScript API docs appears to be what I want, but I cannot find that option in the R package.
To remove the zoom controls, set zoomControl = FALSE
in leafletOptions
. For example:
library(leaflet)
leaflet(options = leafletOptions(zoomControl = FALSE)) %>%
addTiles()
Note that this will not disable zooming via double-clicking or scrolling with your mouse wheel. You can control the zoom level by setting minZoom
and maxZoom
, again in leafletOptions
. To disable zooming, set minZoom
equal to maxZoom
:
leaflet(options = leafletOptions(zoomControl = FALSE,
minZoom = 3, maxZoom = 3)) %>%
addTiles()
As a bonus, in case you want a "static" view of a map, you can also disable dragging via the dragging
option:
leaflet(options = leafletOptions(zoomControl = FALSE,
minZoom = 3, maxZoom = 3,
dragging = FALSE)) %>%
addTiles()
Note that you may need to install the latest github version of leaflet
to implement the above options:
# install github version of leaflet
if (!require('devtools')) install.packages('devtools')
devtools::install_github('rstudio/leaflet')`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With