R Shiny Leaflet 标记缩放问题

R Shiny Leaflet Marker Zoom Issues

我希望能够双击自定义标记并使地图居中和缩放。我不确定它是否应该自动执行而只是没有,或者我是否需要特殊代码。

下面是我当前的标记代码: 谢谢!

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%")) 
server <- function(input, output){
  output$map <- renderLeaflet({
    m <- leaflet(wins) %>% addProviderTiles(providers$OpenStreetMap)
    m %>% setView(-72.690940, 41.651426, zoom = 12)
    m %>% addMarkers(~lng, ~lat,
      icon = greenLeafIcon,
      popup = paste("Where:", wins$Where,"<br>", "What:", wins$What,"<br>", "Who:", wins$Who,"<br>", "Why:", wins$Why,"<br>", "Order Date:", wins$OrderDate,"<br>", "Go Live Date:", wins$GoLiveDate, "<br>","<a href='",wins$link,"' target='_blank'>",wins$link,"</a>"),
      clusterOptions = markerClusterOptions(color="#0017bb"),
      centerMap
      label = wins$Name,
      labelOptions = labelOptions(noHide = F)) 
  })
}
shinyApp(ui, server)

> dput(wins)
structure(list(Name = structure(c(9L, 6L, 11L, 4L, 2L, 10L, 5L, 
3L, 8L, 1L, 7L), .Label = c("Brigam", "Brown", "Buffalo", "Cornell", 
"Maine Medical", "MGH", "Middlesex", "North Maine", "Tufts", 
"UVM", "Yale"), class = "factor"), Where = structure(c(9L, 6L, 
11L, 4L, 2L, 10L, 5L, 3L, 8L, 1L, 7L), .Label = c("Brigam", "Brown", 
"Buffalo", "Cornell", "Maine Medical", "MGH", "Middlesex Medical Hospital", 
"North Maine", "Tufts", "UVM", "Yale"), class = "factor"), What = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "saple3", class = "factor"), 
    Who = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = "RML-  SAM-", class = "factor"), 
    Why = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = "sample", class = "factor"), OrderDate = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Q1", class = "factor"), 
    GoLiveDate = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L), .Label = "Q2", class = "factor"), lat = c(42.349598, 
    42.361542, 41.303229, 40.764991, 41.819064, 44.47972, 43.653243, 
    42.886447, 47.140694, 42.33597, 41.55466), lng = c(-71.063541, 
    -71.0688334, -72.933826, -73.95479, -71.408277, -73.194072, 
    -70.276184, -78.87836, -68.269043, -71.10766, -72.64815), 
    link = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    3L), .Label = c("http:\\www.google.com", "http:\\www.google.org", 
    "https://google.com"
    ), class = "factor")), .Names = c("Name", "Where", "What", 
"Who", "Why", "OrderDate", "GoLiveDate", "lat", "lng", "link"
), class = "data.frame", row.names = c(NA, -11L))

不幸的是 AFAIK 传单中没有 double_click 事件(我很想被证明是错误的)。

如果您点击一下没问题,我们可以这样做:

server <- function(input, output){
    output$map <- renderLeaflet({
        m <- leaflet(wins) %>% 
            addProviderTiles(providers$OpenStreetMap) %>% 
            addMarkers(~lng, ~lat,
                       #icon = 'greenLeafIcon',
                       popup = paste("Where:", wins$Where,"<br>", "What:", wins$What,"<br>", "Who:", wins$Who,"<br>", "Why:", wins$Why,"<br>", "Order Date:", wins$OrderDate,"<br>", "Go Live Date:", wins$GoLiveDate, "<br>","<a href='",wins$link,"' target='_blank'>",wins$link,"</a>"),
                       clusterOptions = markerClusterOptions(color="#0017bb"),
                       #centerMap,
                       label = ~Name,
                       labelOptions = labelOptions(noHide = F)) 
    })

    observe({
        click <- input$map_marker_click
        zoom <- isolate(input$map_zoom)
        if(is.null(click))
            return()

        leafletProxy('map') %>% 
            setView(click$lng, click$lat, zoom = zoom)
    })
}

它的作用是 observe map_marker_click 事件,发生时 leafletProxy() 会将 'map' output 更新为指定的 lnglatzoom。我们需要隔离 input$zoom,否则每次我们更改缩放时 observe 都会启动,从而导致奇怪的行为。