当前鼠标位置在传单地图上的坐标,闪亮

问题描述 投票:0回答:3

我想以闪亮的方式访问传单地图中的当前鼠标位置。使用shiny时,您可以使用

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)
javascript r shiny leaflet
3个回答
7
投票

使用 onRender

 中的 
htmlwidgets
,您可以基于 本文
 添加一些 javascript 将坐标从 
mousemove 传递到 Shiny 输入。

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


0
投票

为什么R传单无法识别

mousemove
?我们应该能够:

observeEvent(input$map_mousemove, {

    coords <- unlist(input$map_mousemove)
})

并相应地提取经度和纬度。 无论如何,这个观察者与

click
事件一起工作,这也是 地图交互事件

的一部分

0
投票
map.on('mousemove',function(e1){
    var lat=e1.latlng.lat;//get live coordinates of mouse movement
    var lng=e1.latlng.lng;
    map.removeLayer(rectangle)//to remove previous loop rectangle
    rectangle=L.rectangle([[l1,l2],[lat,lng]]).addTo(map);
    console.log(lat, lng)
})
© www.soinside.com 2019 - 2024. All rights reserved.