Click event on Leaflet tile map in Shiny

2020-03-19 05:15发布

问题:

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).