Coordinates of the current mouse position on the elevator map with shiny

I want to access the current mouse position on the booklet map in brilliant form. When using brilliant, you can get the current coordinates of the click event using input$MAPID_clickone that contains the latitude and longitude of the click. Similarly, I want to have input$MAPID_mouseovera list of the current latitude and longitude of the mouse cursor.

mapview::addMouseCoordinates(map)displays the coordinates on a flyer map. It uses map.latlng.lng and map.latlng.lat, but I could not figure out how to adapt the code to return a list with 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)
+4
source share
1 answer

onRender htmlwidgets, javascript mousemove , .

library(shiny)
library(leaflet)
library(htmlwidgets)

ui <- fluidPage(
    leafletOutput("map"),
    br(),
    verbatimTextOutput("out")
)

server <- function(input, output, session) {
    output$map <- renderLeaflet({
        leaflet()  %>%
            addProviderTiles("OpenStreetMap.Mapnik") %>%
            setView(-122.4105513,37.78250256, zoom = 12) %>%
            onRender(
                "function(el,x){
                    this.on('mousemove', function(e) {
                        var lat = e.latlng.lat;
                        var lng = e.latlng.lng;
                        var coord = [lat, lng];
                        Shiny.onInputChange('hover_coordinates', coord)
                    });
                    this.on('mouseout', function(e) {
                        Shiny.onInputChange('hover_coordinates', null)
                    })
                }"
            )
    })

    output$out <- renderText({
        if(is.null(input$hover_coordinates)) {
            "Mouse outside of map"
        } else {
            paste0("Lat: ", input$hover_coordinates[1], 
                   "\nLng: ", input$hover_coordinates[2])
        }
    })
}

shinyApp(ui, server)

enter image description here

+1

Source: https://habr.com/ru/post/1685363/


All Articles