在R Shiny中动态更改leaflet地图中心

4

我正在制作一个R Shiny应用程序,用户将输入他们的城市、州和邮政编码,按下一个按钮,然后他们的位置(纬度、经度)将成为地图的新中心。这些输入是通过server.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中leaflet地图的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传递更改后的值。我不确定这是否是作用域问题,但我需要帮助将新的纬度/经度值放入leaflet地图中心。

非常感谢您的任何帮助。提前致谢。

1个回答

4

看起来你正在UI端创建你的leaflet。如果你想让它对输入产生反应,你需要使用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)
})

非常感谢,这就是我在寻找的答案。 - MedEquality

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接