这是indexloc提供的服务,不要输入任何密码
Skip to content

Using tm_view(set_view = ...) in tmapProxy() to centre view on clicked coordinates #1070

@stragu

Description

@stragu

This is probably a question more than a bug report, as I suspect I might not be combining functions in the right way.

I thought I could use tm_view(set_view = ...) in tmapProxy() to centre the map on the clicked coordinates, in the same way as I can remove a layer with tm_remove_layer() and add new layers. But that didn't work.

This is the Shiny application code, which doesn't throw warnings / errors but does not centre the map on every click:

library(shiny)
library(tmap)
library(sf)
tmap_mode("view")

# create initial point data
click_point <- st_point(c(153.004, -27.498))
click_sf <- st_sfc(click_point, crs = "WGS84")

# UI
ui <- fluidPage(
  tmapOutput("map")
)

server <- function(input, output, session) {
  # draw tmap
  output$map <- renderTmap({
    tm_shape(click_sf) +
      tm_dots(zindex = 401)
  })
  
  # observe click
  observeEvent(input$map_click, {
    click_point <- st_point(c(input$map_click$lng, input$map_click$lat))
    click_sf <- st_sfc(click_point, crs = "WGS84")
    # remove and redraw layer
    tmapProxy("map", session, {
      tm_remove_layer(401) +
        tm_shape(click_sf) +
        tm_dots(zindex = 401) +
        # ... and try to centre view on click coordinates
        tm_view(set_view = c(input$map_click$lng,
                             input$map_click$lat,
                             5))
    })
  })
}	

shinyApp(ui, server)

This is using the current dev source (4.0.0.9000).

Metadata

Metadata

Assignees

No one assigned

    Labels

    ShinyIssues related to shiny

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions