从R小册子地图中获取绘图对象的坐标

时间:2018-02-19 01:49:46

标签: r leaflet

我正在构建一个闪亮的应用程序,我想从传单地图中获取多边形的坐标。具体来说,使用leaflet.extras包中的Drawtoolbar绘制形状。下面是一个简单的示例应用程序。

我的问题是,如何从用户在地图上绘制的形状中获取坐标?提前谢谢。

library(shiny)
library(leaflet)
library(leaflet.extras)

# Define UI 
ui <- fluidPage(
  leafletOutput("mymap",height=800)
)

# Define server logic 
server <- function(input, output) {
  output$mymap <- renderLeaflet(
    leaflet() %>%
    addProviderTiles("Esri.OceanBasemap",group = "Ocean Basemap") %>%
    setView(lng = -166, lat = 58.0, zoom = 5) %>%
    addDrawToolbar(
      targetGroup='draw',
      editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions()))  %>%
  addLayersControl(overlayGroups = c('draw'), options =
                     layersControlOptions(collapsed=FALSE))
)

observeEvent(input$mymap_shape_click,{
   print(input$mymap_shape_click)
})

  observeEvent(input$mymap_click,{
    print(input$mymap_click)
  })
}

# Run the application
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

您需要观察_draw_new_feature功能

library(leaflet.extras)

# Define UI 
ui <- fluidPage(
  leafletOutput("mymap",height=800)
)

# Define server logic 
server <- function(input, output) {

  output$mymap <- renderLeaflet(
    leaflet() %>%
      addProviderTiles("Esri.OceanBasemap",group = "Ocean Basemap") %>%
      setView(lng = -166, lat = 58.0, zoom = 5) %>%
      addDrawToolbar(
        targetGroup='draw',
        editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions()))  %>%
      addLayersControl(overlayGroups = c('draw'), options =
                         layersControlOptions(collapsed=FALSE))
  )

  observeEvent(input$mymap_draw_new_feature,{
    feature <- input$mymap_draw_new_feature

    print(feature)

  })

}

# Run the application
shinyApp(ui = ui, server = server)