Get Map Position and Zoom Level from sync'd Leaflet Maps

Using shiny, leaflet, and leafsync I'm building a group of maps and displaying them on a web page. A user of the page can change the number of maps by setting values for a group of selection criteria. It's working well enough, but I'm building the maps on-the-fly, and the position and zoom level of the maps revert to their initial values when the number of maps changes. Here's a very simplified version of my app.

library(shiny)
library(leaflet)
library(leafsync)

ui <- fluidPage(

# Application title
titlePanel("Display N Maps"),

#  Sidebar with a slider input for number of maps
sidebarLayout(
    sidebarPanel(
        sliderInput("mapCount",
                    "Number of maps:",
                    min = 1,
                    max = 4,
                    value = 2)
    ),

    # Display a dynamically created set of maps
    mainPanel(
        uiOutput("tiledMaps")
    )
)

)

server <- function(input, output) {

map.rv <- reactiveValues()
map.rv$count <- 0
map.rv$zoom <- 9
map.rv$ctrLng <- 174.768
map.rv$ctrLat <- -36.852

tileMap <- function(i) {
    leaflet(paste0("map",i)) %>%
        addTiles() %>%
        setView(lng = map.rv$ctrLng,
                lat = map.rv$ctrLat,
                zoom = map.rv$zoom)
}

output$tiledMaps <- renderUI({
    mapArray <- apply(as.array(1:input$mapCount), 1, 
                      function(i){tileMap(i)})
    sync(mapArray)
})

}

shinyApp(ui = ui, server = server)

If I knew the current values for the sync'd maps position and zoom, I could set the reactive variables and build maps that match, but I can't figure out how to access the the equivalent of the input$MAPID_zoom or input$MAPID_center values when the maps are inside a sync.

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.