Leaflet R shiny: select & zoom

后端 未结 1 2004
感情败类
感情败类 2021-01-27 11:02

I\'m working on a tool with shiny and leaflet: I want that when a customer click on the vars (see UI code for exemple NE), the map go to another view for exemple like this in pu

相关标签:
1条回答
  • 2021-01-27 11:14

    To update a leafletMap you should use leafletProxy, you can read about that here.

    The idea is that you need to have a reactive value that is updated when you change the selection, that reactive value is observed by the leafletProxy and its values used to perform the update.

    It should look like this:

    output$map <- renderLeaflet({
            leaflet(data) %>%
                addTiles(urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
                         attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>'
                ) %>% 
                setView(lng = 6.6328200, lat = 46.5160000, zoom = 12) %>% 
                addMarkers(data$long, data$lat, label = data$nom)            
        })
    
        center <- reactive({
            subset(data, nom == input$canton) 
            # or whatever operation is needed to transform the selection 
            # to an object that contains lat and long
        })
    
        observe({
            leafletProxy('map') %>% 
                setView(lng =  center()$long, lat = center()$lat, zoom = 12)
        })
    
    0 讨论(0)
提交回复
热议问题