英文:
Make Tmap keep the same viewport when redrawing (R/Shiny)
问题
我想存储Tmap中的中心坐标和缩放级别,以便在地图重绘时使用最后已知的中心点和缩放级别。当前行为是重置为初始中心点和缩放级别,如下所示:
library(shiny)
library(tmap)
data(World)
world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))
ui <- fluidPage(
tmapOutput("map"),
actionButton("randVar", "Random")
)
server <- function(input, output, session) {
map.center = c(lon=-2.460938,lat=54.444492) # 视口的中心
map.zoom = 4 # 当前缩放级别
rv <- reactiveValues(var = 1)
output$map <- renderTmap({
tmap_mode("view")
tm_shape(World) +
tm_polygons(world_vars[rv$var]) +
tm_view(set.view = c(map.center['lon'], map.center['lat'], zoom=map.zoom))
})
## ---------------
observeEvent(input$map_bounds, {
map.zoom <- input$map_zoom
map.center['lon'] <- input$map_center[['lng']]
map.center['lat'] <- input$map_center[['lat']]
print(map.center)
print(map.zoom)
})
## ---------------
observeEvent(input$randVar, {
rv$var <- sample(1:length(world_vars), 1)
})
}
shinyApp(ui, server)
缩放或平移地图,然后按“Random”按钮。
期望的行为是地图重新绘制,并且set.view使用最后的中心点和缩放级别,但它重置为初始设置?
英文:
I'd like to store the centre coordinates and zoom in Tmap so that when the map gets redrawn it uses the last known centre point and zoom level. The current behaviour is that it resets to the initial centre point and zoom level as per repro:
library(shiny)
library(tmap)
data(World)
world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))
ui <- fluidPage(
tmapOutput("map"),
actionButton("randVar", "Random")
)
server <- function(input, output, session) {
map.center = c(lon=-2.460938,lat=54.444492) # The center of the viewport
map.zoom = 4 # The current zoom level
rv <- reactiveValues(var = 1)
output$map <- renderTmap({
tmap_mode("view")
tm_shape(World) +
tm_polygons(world_vars[rv$var]) +
tm_view(set.view = c(map.center['lon'], map.center['lat'], zoom=map.zoom))
})
## ---------------
observeEvent(input$map_bounds, {
map.zoom <- input$map_zoom
map.center['lon'] <- input$map_center[['lng']]
map.center['lat'] <- input$map_center[['lat']]
print(map.center)
print(map.zoom)
})
## ---------------
observeEvent(input$randVar, {
rv$var <- sample(1:length(world_vars), 1)
})
}
shinyApp(ui, server)
Zoom or pan the map and then press 'Random'.
The expected behaviour is for the map to redraw and for set.view to use the last centre point and zoom, but it resets to its initial setting?
答案1
得分: 1
library(shiny)
library(tmap)
data(World)
world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))
ui <- fluidPage(
tmapOutput("map"),
actionButton("randVar", "Random")
)
server <- function(input, output, session) {
map.center <<- c(lon=-2.460938,lat=54.444492) # 视口中心
map.zoom <<- 4 # 当前缩放级别
rv <- reactiveValues(var = 1)
output$map <- renderTmap({
tmap_mode("view")
tm_shape(World) +
tm_polygons(world_vars[rv$var]) +
tm_view(set.view = c(map.center['lon'], map.center['lat'], zoom=map.zoom))
})
## ---------------
observeEvent(input$map_bounds, {
map.zoom <<- input$map_zoom
map.center['lon'] <<- input$map_center[['lng']]
map.center['lat'] <<- input$map_center[['lat']]
print(map.center)
print(map.zoom)
})
## ---------------
observeEvent(input$randVar, {
rv$var <- sample(1:length(world_vars), 1)
})
}
shinyApp(ui, server)
英文:
library(shiny)
library(tmap)
data(World)
world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))
ui <- fluidPage(
tmapOutput("map"),
actionButton("randVar", "Random")
)
server <- function(input, output, session) {
map.center <<- c(lon=-2.460938,lat=54.444492) # The center of the viewport
map.zoom <<- 4 # The current zoom level
rv <- reactiveValues(var = 1)
output$map <- renderTmap({
tmap_mode("view")
tm_shape(World) +
tm_polygons(world_vars[rv$var]) +
tm_view(set.view = c(map.center['lon'], map.center['lat'], zoom=map.zoom))
})
## ---------------
observeEvent(input$map_bounds, {
map.zoom <<- input$map_zoom
map.center['lon'] <<- input$map_center[['lng']]
map.center['lat'] <<- input$map_center[['lat']]
print(map.center)
print(map.zoom)
})
## ---------------
observeEvent(input$randVar, {
rv$var <- sample(1:length(world_vars), 1)
})
}
shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论