小册子地图上当前鼠标位置的坐标与闪亮 [英] Coordinates of current mouse position on leaflet map with shiny

查看:98
本文介绍了小册子地图上当前鼠标位置的坐标与闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

 库(闪亮)
库(传单)

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


server< - function(input,output,session){
output $ map< - renderLeaflet({
leaflet()%>%addTiles()
})

输出$ out< - renderPrint({
validate(需要(输入$ map_mouseover,FALSE))
str(输入$ map_mouseover)
})
}

shinyApp(ui,server)


解决方案

使用


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)

解决方案

Using onRender from htmlwidgets, you can add some javascript to pass the coordinates from mousemove to a Shiny input, based on this article.

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)

这篇关于小册子地图上当前鼠标位置的坐标与闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆