2017-09-18 5 views
1

イージーボタンを使用して、Shiny-Leaflet環境でボタンを作成し、地図をズームしてユーザーの地理的位置に移動します。実例:R/Shiny/Leaflet:ユーザーの地理的位置をキャプチャして記録する

library(shiny) 
library(leaflet) 

ui <- fluidPage(leafletOutput("map")) 


server <- function(input, output, session) { 

output$map <- renderLeaflet({ 
leaflet() %>% 
    addProviderTiles(providers$Stamen.TonerLite, 
        options = providerTileOptions(noWrap = TRUE)) %>% 

    addEasyButton(
    easyButton(
     position = "topleft", 
     icon = "fa-crosshairs", 
     title = "Locate Me", 
     onClick = JS(
     c(
      "function(btn, map){map.locate({setView:true,enableHighAccuracy: true })}" 
     ) 
    ) 
    ) 
    ) 
    }) 
} 

shinyApp(ui,server) 

これは問題なく動作します。しかし、ユーザーの地理的位置の緯度/経度を入力変数に取り込みたいと思います。この機能は、マップ上でマウスをクリックすると実行されます。その場合、クリックされた位置の緯度/経度は 'input $ map_click'に格納されます。誰にもアイデアはありますか?

答えて

0

出発点として、あなたは変化するマップの境界を観察し、境界

library(shiny) 
library(leaflet) 

ui <- fluidPage(leafletOutput("map")) 


server <- function(input, output, session) { 

    output$map <- renderLeaflet({ 
    leaflet() %>% 
     addProviderTiles(providers$Stamen.TonerLite, 
         options = providerTileOptions(noWrap = TRUE)) %>% 

     addEasyButton(
     easyButton(
      position = "topleft", 
      icon = "fa-crosshairs", 
      title = "Locate Me", 
      onClick = JS(
      c(
       "function(btn, map){map.locate({setView:true,enableHighAccuracy: true })}" 
      ) 
     ) 
     ) 
    ) 
    }) 

    observeEvent(input$map_bounds, { 
    event <- input$map_bounds 

    lat <- mean(event$north, event$south) 
    lon <- mean(event$west, event$east) 

    print(paste0("map center - lat: ", lat, ", lon: ", lon)) 

    }) 
} 

shinyApp(ui,server) 

の中心を返すことができますただし、マップがパンされるたび座標を取得します。あなたは、ボタンを押したときにのみこれを観察することができますが、現時点では私はそれをどうするかわかりません。

関連する問題