【问题标题】:Dynamically change leaflet map center in R Shiny在 R Shiny 中动态更改传单地图中心
【发布时间】:2015-09-02 16:19:27
【问题描述】:

我正在制作一个 R Shiny 应用程序,用户将在其中输入他们的城市、州和邮政编码,按下一个按钮,然后他们的位置(经纬度)将成为地图的新中心。输入是通过服务器中的这部分代码收集的。R:

output$ntextCounter <- eventReactive(input$goButton, {
   citySelected <- as.character(input$city)
   stateSelected <- as.character(input$state)
   zipCodeSelected <- as.character(input$zipCode)
   location2 <- stri_detect(paste(as.character(input$city),
   as.character(input$state), as.character(input$zipCode), sep=", "), fixed = locationData$Location, opts_regex=stri_opts_regex(case_insensitive=TRUE))
   counter <<- counter + 1
   lat1 <- as.numeric(locationData[which(location2),]$latitude)
   lon1 <- as.numeric(locationData[which(location2),]$longitude)
   return(c(lat1, lon1))
})

我可以使用以下方法在 UI 中轻松查看新的纬度/经度值:

 verbatimTextOutput("ntextCounter")

但我需要能够将这些值“return(c(lat1, lon1))”传递到 ui.r 的传单地图中的 center = c(Lat, Lon):

leafletMap("map", "100%", 365,
    initialTileLayer = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
    initialTileLayerAttribution = HTML('&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'),
    options=list(center = c(38.25, -93.85), zoom = 4, maxBounds = list(list(1, -180), list(78, 180))
    )),

我在 c(38.25, -93.85) 有一个初始地图中心,但最终我希望能够从 ntextCounter 传递它的变化值。我不确定这是范围界定问题还是什么,但我需要帮助将新的纬度/经度值放入传单地图中心。

任何帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: r maps parameter-passing shiny leaflet


    【解决方案1】:

    看来您是在 ui 端创建传单。如果您希望它响应输入,您必须在服务器端使用renderLeaflet 执行此操作。

    您的坐标可以存储在reactiveValues 中,您可以使用observeEvent 更新它们:

    location <- reactiveValues(lat = 38.25, lon = -93.85)
    observeEvent(input$goButton, {
               city <- as.character(input$city)
               state <- as.character(input$state)
               zipCode <- as.character(input$zipCode)
               newloc <- stri_detect(paste(city, state, zipCode, sep=", "), 
                                        fixed = locationData$Location, 
                                        opts_regex=stri_opts_regex(case_insensitive=TRUE))
              location$lat <- as.numeric(locationData[which(newloc),]$latitude)
              location$lon <- as.numeric(locationData[which(newloc),]$longitude)
    })
    

    【讨论】:

    • 非常感谢,这就是我正在寻找的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 2018-08-31
    • 2019-12-30
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多