本文介绍了从R传单地图中获取绘图对象的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a shiny app where I would like to get the coordinates of a polygon from a leaflet map. Specifically, the shape is drawn using the Drawtoolbar from the leaflet.extras package. A simple example app is below.

我的问题是,如何从用户在地图上绘制的形状中获取坐标?预先谢谢你.

My question is, how can I get the coordinates from the shape drawn on the map by the user? Thank you in advance.

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)

推荐答案

您需要观察 _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)

这篇关于从R传单地图中获取绘图对象的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:53