【发布时间】:2021-10-24 23:22:55
【问题描述】:
我正在一个闪亮的应用程序中绘制瑞士的自行车事故。按州过滤事故,但我想在每次选择新州时重新定位地图。我认为 flyTo() 可以解决问题,但我无法让它发挥作用。
if(!require('leaflet')){install.packages('leaflet'); library('leaflet')}
if(!require('shiny')){install.packages('shiny'); library('shiny')}
if(!require('tidyverse')){install.packages('tidyverse'); library('tidyverse')}
veloaua <- read_csv(file = 'https://raw.githubusercontent.com/nicoschreibt/velovaua/master/veloaua_github.csv?token=AP64ETIQHSXQADG2GKFM6B3BEVLM6')
veloaua <- veloaua[sample(1:length(veloaua$AccidentType), size = 100),]
ui <- fluidPage(
selectInput(inputId = "kant",
label = "Welchen Kanton willst du sehen?",
choices = unique(veloaua$CantonCode)),
leafletOutput("mymap")
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Stamen.Toner, options = providerTileOptions(minZoom = 8, maxZoom = 20))%>%
clearBounds() %>%
addMarkers(data = veloaua,
lng = ~wgs84_e,
lat = ~wgs84_n,
icon = icons,
popup = veloaua$poptext,
group = "main")
})
observeEvent(input$kant, {
leafletProxy("mymap")%>%
clearGroup("main") %>%
addMarkers(data = veloaua[veloaua$CantonCode == input$kant,],
lng = ~wgs84_e,
lat = ~wgs84_n,
popup = ~poptext,
group = "main",
clusterOptions = markerClusterOptions(
removeOutsideVisibleBounds = TRUE,
showCoverageOnHover = FALSE,
disableClusteringAtZoom = 15)) %>%
leaflet::flyTo(map = "mymap",
lng = veloaua$wgs84_e[veloaua$CantonCode == input$kant],
lat = veloaua$wgs84_n[veloaua$CantonCode == input$kant])
}
)
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
请提供reproducible minimal example。特别是,提供一些示例数据,例如与
dput() -
您是否尝试过管道到 setView()?您可以设置地图的经纬度中心。
-
好点。也许你可以让它像这样工作。
-
我不想要地图的中心,而是我设置的标记之一。这就是它的全部意义 =)