I want to access the current mouse position in a leaflet map in shiny. When using shiny you can get the current coordinates of a click event using input$MAPID_click
, which contains latitude and longitude of the click. Similarly I want to have input$MAPID_mouseover
with a list of the current latitude and longitude of the mouse cursor.
mapview::addMouseCoordinates(map)
displays the coordinates in the leaflet map. It uses map.latlng.lng and map.latlng.lat, but I couldn't figure out, how to adapt the code to return a list with the coordinates instead of displaying them.
Ideally this code should work:
library(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput("map"),
br(),
verbatimTextOutput("out")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>% addTiles()
})
output$out <- renderPrint({
validate(need(input$map_mouseover, FALSE))
str(input$map_mouseover)
})
}
shinyApp(ui, server)