Search code examples
rshinyr-leaflet

How to get GPS coordinates with leaflet.extras addControlGPS


I'm using the leaftlet.extras R package to add Gps control inside a map. I'm using the extension addControlGPS inside my code:

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

The controller works ok.

I need to extract the Gps coordinates to re-use in my code as arguments for other functions. Is there any way to do that?


Solution

  • Every time the gps location updates, the coordinates are written to map.id+'_gps_located'. You can find all leaflet.extras bindings in the htmlwidgets/bindings folder in their git.

    Working example

    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)