Leaflet: Restrict the viewing bounds of a leaflet map in R

Created on 20 Jan 2018  Â·  4Comments  Â·  Source: rstudio/leaflet

Goal

How can I correct my code so that user cannot zoom or drag the leaflet map away from the viewing bounds I set with the leaflet::setMaxBounds() function?

Code

```R
# install GitHub Version of Leaflet
if( !require( package = "devtools") ) install.packages( pkgs = "devtools" )
devtools::install_github('rstudio/leaflet')

# load leaflet package
library( leaflet )

# create leaflet map
myMap <- leaflet() %>%
  addTiles() %>%
  setView( lng = -87.567215
           , lat = 41.822582
           , zoom = 11 ) %>%
  setMaxBounds( lng1 = -87.94011
                , lat1 = 41.64454
                , lng2 = -87.52414
                , lat2 = 42.02304 )

# display leaflet map
myMap
# Problem

When I run the following code down up above, the map zooms into the location I specified in `leaflet::setZiew()`.

[![SS of Actual Map][1]][1]

Ideally, the user would only be able to view the map within the bounds I specified using `leaflet::setMaxBounds()`. Despite the use of that specific function, the user can zoom out as far as they want.

Any help would be much appreciated!

[![SS of Problem With Leaflet Map][2]][2]

# Session Info

    R version 3.4.3 (2017-11-30)
    Platform: x86_64-apple-darwin15.6.0 (64-bit)
    Running under: macOS Sierra 10.12.6

    Matrix products: default
    BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
    LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     

    other attached packages:
    [1] leaflet_1.1.0.9000 devtools_1.13.4   

    loaded via a namespace (and not attached):
     [1] Rcpp_0.12.14    digest_0.6.13   withr_2.1.1     mime_0.5       
     [5] R6_2.2.2        jsonlite_1.5    xtable_1.8-2    magrittr_1.5   
     [9] rstudioapi_0.7  tools_3.4.3     htmlwidgets_0.9 crosstalk_1.0.0
    [13] shiny_1.0.5     httpuv_1.3.5    yaml_2.1.16     compiler_3.4.3 
    [17] memoise_1.1.0   htmltools_0.3.6

# RStudio Version

```R
$citation

To cite RStudio in publications use:

  RStudio Team (2016). RStudio: Integrated Development for R. RStudio,
  Inc., Boston, MA URL http://www.rstudio.com/.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {RStudio: Integrated Development Environment for R},
    author = {{RStudio Team}},
    organization = {RStudio, Inc.},
    address = {Boston, MA},
    year = {2016},
    url = {http://www.rstudio.com/},
  }


$mode
[1] "desktop"

$version
[1] ‘1.1.414’

Most helpful comment

Hello there,

This solution works only for one screen size. How can this be replicated automatically on different screen size. For instance i set minzoom level on my laptop to 11 and when i use same code and run on my desktop at work boom it displays some of the other part of the map because my screen size at work is bigger than my laptop. How can this be achieved?

All 4 comments

If I understand you correctly, you want to prevent the user from zooming out and only allow zooming in. You can specify minZoom and maxZoom in the leafletOptions to achieve this.

library(leaflet)

myMap <- leaflet(options = leafletOptions(minZoom = 11)) %>%
  addProviderTiles("OpenStreetMap") %>%
  setView( lng = -87.567215
           , lat = 41.822582
           , zoom = 11 ) %>%
  setMaxBounds( lng1 = -87.94011
                , lat1 = 41.64454
                , lng2 = -87.52414
                , lat2 = 42.02304 )

# display leaflet map
myMap

@tim-salabim, thank you! This is exactly what I was looking for to solve this issue.

Greyed out Zoom Out Button

I appreciate your help and will close this issue.

Hello there,

This solution works only for one screen size. How can this be replicated automatically on different screen size. For instance i set minzoom level on my laptop to 11 and when i use same code and run on my desktop at work boom it displays some of the other part of the map because my screen size at work is bigger than my laptop. How can this be achieved?

Hello there,

This solution works only for one screen size. How can this be replicated automatically on different screen size. For instance i set minzoom level on my laptop to 11 and when i use same code and run on my desktop at work boom it displays some of the other part of the map because my screen size at work is bigger than my laptop. How can this be achieved?

Hello Noone to help me on this? Please.

Was this page helpful?
0 / 5 - 0 ratings