How to get zoom level from booklet map in R / shiny?

I am creating a map using a booklet package in Shiny that has selectInput so that the user can select from a list of sites. A list of sites is also added to the flyer as markers.

When a user selects a new site, I want to map the map to the selected site without changing the zoom level. The setView function can be called to set the center points, but should indicate the level of zoom.

Is it possible to get the zoom level of the flyer map that can be used in the setView function?

This is a minimal example for playing with my question with reset zoom level.

 library(shiny) library(leaflet) df <- data.frame( site = c('S1', 'S2'), lng = c(140, 120), lat = c(-20, -30), stringsAsFactors = FALSE) # Define UI for application that draws a histogram ui <- shinyUI(fluidPage( selectInput('site', 'Site', df$site), leafletOutput('map') )) server <- shinyServer(function(input, output, session) { output$map <- renderLeaflet({ leaflet() %>% addTiles() %>% setView(lng = 133, lat = -25, zoom = 4) %>% addMarkers(lng = df$lng, lat = df$lat) }) observe({ req(input$site) sel_site <- df[df$site == input$site,] isolate({ leafletProxy('map') %>% setView(lng = sel_site$lng, lat = sel_site$lat, zoom = 4) }) }) }) shinyApp(ui = ui, server = server) 

PS: when you play with these codes, adjust the zoom level before choosing a new site.

Thanks for any suggestions.

+7
r shiny leaflet
source share
1 answer

You can access the zoom level with input$mapid_zoom ( see here ).

In your observe you can do:

  observe({ sel_site <- df[df$site == input$site,] isolate({ new_zoom <- 4 if(!is.null(input$map_zoom)) new_zoom <- input$map_zoom leafletProxy('map') %>% setView(lng = sel_site$lng, lat = sel_site$lat, zoom = new_zoom) }) }) 
+7
source share

All Articles