首页 > 解决方案 > 如何在闪亮中获取和设置传单(代理)地图的中心位置

问题描述

我想检索当前中心位置并将其设置为相同的numericInput. 一般来说,我能够实现它,但是,在更改输入字段时,传单很难到达稳定的位置,但会来回弹跳。

我还尝试包含一个短暂的延迟 ( throttle()/ debounce()) 来规避问题,但事实证明这是不成功的。有什么想法可以让它双向工作吗?

library(shiny)
library(shinyWidgets)
library(leaflet)

ui <- fluidPage(
  fluidRow(
  numericInputIcon(inputId = "longitude",
                   label = "Longitude",
                   icon = icon("arrows-alt-h"),
                   min = -180,
                   max = 180,
                   value = 18,
                   step = .1),
  numericInputIcon(inputId = "latitude",
                   label = "Latitude",
                   icon = icon("arrows-alt-v"),
                   min = -90,
                   max = 90,
                   value = 50,
                   step = .1)
  ),
  leafletOutput("map")
)
server <- function(input, output, session) {

  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      setView(lng = isolate(input$longitude),
              lat = isolate(input$latitude),
              zoom = 3)
  })

  observeEvent(input$map_center, {
    updateNumericInputIcon(session = session,
                           inputId = "longitude",
                           value = round(input$map_center$lng, 1))
    updateNumericInputIcon(session = session,
                           inputId = "latitude",
                           value = round(input$map_center$lat, 1))
  })


  # with delay -------------------------------------
  updateMap <- reactive({
    leafletProxy("map", session) %>%
      setView(lng = input$longitude,
              lat = input$latitude,
              zoom = 3)
  })

  updateMap_t <- updateMap %>% throttle(1000)

  observe(updateMap_t())

  # without any delay ------------------------------
  # observe({
  #   leafletProxy("map", session) %>%
  #     setView(lng = input$longitude,
  #             lat = input$latitude,
  #             zoom = 3)
  # })
}

shinyApp(ui, server)

旁注:我不想再次渲染完整的地图,因为这需要一些时间。因此,leafletProxy

标签: rshinyleafletshiny-reactivityr-leaflet

解决方案


如果其他人好奇,当将延迟应用于 numericInput 而不是地图更新时,它会更加稳定且实际可用。仍然有轻微的震动,但这是可以挽救的 IMO。任何其他建议仍然值得赞赏。

library(shiny)
library(shinyWidgets)
library(leaflet)

ui <- fluidPage(
  fluidRow(
    numericInputIcon(inputId = "longitude",
                     label = "Longitude",
                     icon = icon("arrows-alt-h"),
                     min = -180,
                     max = 180,
                     value = 18,
                     step = .1),
    numericInputIcon(inputId = "latitude",
                     label = "Latitude",
                     icon = icon("arrows-alt-v"),
                     min = -90,
                     max = 90,
                     value = 50,
                     step = .1)
  ),
  leafletOutput("map")
)
server <- function(input, output, session) {

  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      setView(lng = isolate(input$longitude),
              lat = isolate(input$latitude),
              zoom = 3)
  })



  # update Input with delay
  map_long <- eventReactive(input$map_center$lng, {
    input$map_center$lng
  })%>% debounce(1000)
  map_lat <- eventReactive(input$map_center$lat, {
    input$map_center$lat
  })%>% debounce(1000)

  observe({
    updateNumericInputIcon(session = session,
                           inputId = "longitude",
                           value = round(map_long(), 1))
    updateNumericInputIcon(session = session,
                           inputId = "latitude",
                           value = round(map_lat(), 1))
  })


  # without any delay
  observe({
    leafletProxy("map", session) %>%
      setView(lng = input$longitude,
              lat = input$latitude,
              zoom = 3)
  })



}

shinyApp(ui, server)

推荐阅读