Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R leaflet zoomControl option

Tags:

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.

like image 587
Brian Stamper Avatar asked Apr 01 '16 21:04

Brian Stamper


1 Answers

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')`
like image 139
George Wood Avatar answered Sep 28 '22 07:09

George Wood