首页 > 解决方案 > 如何使用 leaflet.extras addControlGPS 获取 GPS 坐标

问题描述

我正在使用Leaflet.extras R 包在地图中添加 Gps 控件。我在代码中使用了扩展addControlGPS

...   %>% 
addControlGPS(options = gpsOptions(position = "topleft", activate = TRUE, 
                                             autoCenter = TRUE, maxZoom = 60, 
                                             setView = TRUE))  %>%
...

控制器工作正常。

我需要提取 Gps 坐标以在我的代码中重新用作其他函数的参数。有什么办法吗?

标签: rshinyleaflet

解决方案


每次 gps 位置更新时,坐标都会写入map.id+'_gps_located'. 您可以在他们的 gitleaflet.extras中的 htmlwidgets/bindings文件夹中找到所有绑定。

工作示例

library(leaflet)
library(leaflet.extras)
library(shiny)

ui <- fluidPage(
  leafletOutput('map')
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({ leaflet()%>%addTiles() %>% 
      addControlGPS(options = gpsOptions(position = "topleft", activate = TRUE, 
                                         autoCenter = TRUE, maxZoom = 60, 
                                         setView = TRUE))})
  observe(
    print(input$map_gps_located)
  )
}

shinyApp(ui, server)

推荐阅读