21

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.

Brian Stamper
  • 2,143
  • 1
  • 18
  • 41
  • have you tried something like: addProviderTiles("CartoDB.Positron", options=tileOptions(minZoom=9)) or addProviderTiles("CartoDB.Positron", options=tileOptions(maxZoom=9)). You can change the number for what you want. – MLavoie Apr 01 '16 at 21:33
  • the `setMaxBounds( )` stops you from scrolling left/right/up/down (i.e., the map boundaries), not the zoom level. From `?setView` the [zoom options](http://leafletjs.com/reference.html#map-zoompanoptions) available to `options = list( )` don't appear to include *zoomControl*. function – SymbolixAU Apr 02 '16 at 06:28
  • @MLavoie, I checked out the maxZoom/minZoom, and that does come close, but it would be better if I could also remove the zoom buttons. – Brian Stamper Apr 04 '16 at 02:05
  • @Symbolix, ah yes I see that now, not really the behavior I expected. Now I really just wish I could disable any zooming and remove the controls. – Brian Stamper Apr 04 '16 at 02:07
  • @BrianStamper - It should be possible, but you just need to work out how to implement the [`javascript` zoom control](http://stackoverflow.com/questions/16537326/leafletjs-how-to-remove-the-zoom-control) function – SymbolixAU Apr 04 '16 at 02:18

1 Answers1

43

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')`
George Wood
  • 1,914
  • 17
  • 18
  • 1
    This works when you know in advance what the required zoom is, but what if you need to adapt the zoom according to particular variables, e.g. using fitBounds() ? A doubleClickZoom=FALSE or scrollWheelZoom=FALSE in leafletOptions() would be ideal, but this does not seem to be implemented in current versions. – TCW Jan 13 '20 at 11:11