Skip to content

Instantly share code, notes, and snippets.

@ColinFay
Last active August 6, 2018 22:04
Show Gist options
  • Select an option

  • Save ColinFay/3c379ab7f89647111bfeaf4b2abc567c to your computer and use it in GitHub Desktop.

Select an option

Save ColinFay/3c379ab7f89647111bfeaf4b2abc567c to your computer and use it in GitHub Desktop.
geoloc shiny
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)
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