Last active
August 6, 2018 22:04
-
-
Save ColinFay/3c379ab7f89647111bfeaf4b2abc567c to your computer and use it in GitHub Desktop.
geoloc shiny
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(shiny) | |
| library(leaflet) | |
| ui <- fluidPage( | |
| h2("Where Am I?"), | |
| tags$p("Click the button to get your coordinates"), | |
| tags$button(onclick = "getLocation()", "Try it"), | |
| tags$p(id = "where"), | |
| leafletOutput("lf"), | |
| includeScript("www/sc.js") | |
| ) | |
| server <- function(input, output) { | |
| output$lf <- renderLeaflet({ | |
| req(input$lon) | |
| req(input$lat) | |
| leaflet() %>% | |
| addTiles() %>% | |
| setView(as.numeric(input$lon), as.numeric(input$lat), zoom = 17) %>% | |
| addMarkers(as.numeric(input$lon), as.numeric(input$lat), label = "You're here!") | |
| }) | |
| } | |
| shinyApp(ui = ui, server = server) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var x = document.getElementById("where"); | |
| function getLocation() { | |
| if (navigator.geolocation) { | |
| navigator.geolocation.getCurrentPosition(showPosition); | |
| } else { | |
| x.innerHTML = "Geolocation is not supported by this browser."; | |
| } | |
| } | |
| function showPosition(position) { | |
| Shiny.onInputChange("lat", position.coords.latitude); | |
| Shiny.onInputChange("lon", position.coords.longitude); | |
| x.innerHTML = "Latitude: " + position.coords.latitude + | |
| "<br>Longitude: " + position.coords.longitude; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment