R Shiny Leaflet 标记缩放问题

标签 r shiny leaflet markers

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

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

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)
    })
}

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

关于R Shiny Leaflet 标记缩放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44928273/

相关文章:

基于 SelectInput 的 R Shiny 数据表突出显示行

leaflet - 用传单风水师在多边形上打洞

r - 如何使用 dplyr 在不丢失 NA 行的情况下过滤数据

r - 将数据框转换为深度嵌套列表

r - ggplot 比例变换对点和函数的作用不同

r - ggplot2 : How to change axis/tick thickness中的图形参数

r - 在 Shiny 的应用程序中为 rbokeh 图设置 xlim

r - 如何使用传单读取 Shiny 的标记数据

css - 使其他标记位于自定义 divIcon 弹出窗口下方,而不是位于顶部

javascript - 在传单中隐藏工具提示以获得缩放范围