14

Is it possible to get the lat long from a click event in leaflet/shiny (in R) from the tile map? (i.e. NOT from any loaded markers, polygons etc). Just to show positional (lat/long) info i guess.

I thought maybe from this qu it was possible but no luck.

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%")
)

server <- function(input, output,session) {

  output$map <- renderLeaflet({
    leaflet() %>% 
      addProviderTiles("CartoDB.Positron")%>%
      setView(lng = -4, lat= 52.54, zoom = 7)
  })

  #Show popup on click
  observeEvent(input$map_marker_click, {
    click <- input$map_marker_click
    text<-paste("Lattitude ", click$lat, "Longtitude ", click$lng)

    proxy <- leafletProxy("map")
    proxy %>% clearPopups() %>%
      addPopups(click$lng, click$lat, text)
  })

}

runApp(shinyApp(ui, server), launch.browser = TRUE)

Ultimately i want to create a click marker for raster data (using the returned lat/long) in Leaflet & Shiny but this would be a good start (This qu seems to have done something but i cannot recreate it at all).

Community
  • 1
  • 1
Sam
  • 1,400
  • 13
  • 29
  • 11
    This event `input$MAPID_click` has the lat and lng from a click anywhere in the map so you can use it to get what you want. In your `observeEvent`, replace `input$map_marker_click` by `input$map_click` and it should work. This [page](https://rstudio.github.io/leaflet/shiny.html) has a lot of info about all the events. – NicE Feb 28 '17 at 11:51
  • 1
    @NicE, i had a gut feeling this would be simple.... thanks so much – Sam Feb 28 '17 at 12:14

0 Answers0