当前鼠标位置在传单地图上的坐标,有光泽

2023-12-25

我想以闪亮的方式访问传单地图中的当前鼠标位置。使用闪亮时,您可以使用以下命令获取单击事件的当前坐标input$MAPID_click,其中包含点击的纬度和经度。同样我想要有input$MAPID_mouseover包含鼠标光标当前纬度和经度的列表。

mapview::addMouseCoordinates(map)显示传单地图中的坐标。它使用map.latlng.lng和map.latlng.lat,但我无法弄清楚如何调整代码以返回带有坐标的列表而不是显示它们。

理想情况下,这段代码应该有效:

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)

Using onRender https://www.rdocumentation.org/packages/htmlwidgets/versions/1.0/topics/onRender from htmlwidgets,您可以添加一些 javascript 来传递坐标mousemove http://leafletjs.com/reference-1.3.0.html#map-mousemove到一个闪亮的输入,基于本文 https://shiny.rstudio.com/articles/js-send-message.html.

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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当前鼠标位置在传单地图上的坐标,有光泽 的相关文章

随机推荐