R传单如何在地图上单击并添加圆圈 [英] R leaflet how to click on map and add a circle

查看:77
本文介绍了R传单如何在地图上单击并添加圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我已经尝试了好几个星期,但是我却无法完成它. R传单的在线资源也不够.确实需要完成此操作.

Hi guys I have been trying this for weeks but I couldn't manage to get it done. Online resources for R leaflet are not enough too. Really need to get this done.

请帮助,非常感谢.

ui.R->

library(shiny)
library(ggmap)
library(leaflet)

shinyUI(bootstrapPage(
  leafletOutput("map"),
  br(),
  verbatimTextOutput("out")
)
)

server.R->

server.R -->

library(shiny)
library(ggmap)
library(leaflet)


shinyServer(function(input, output, session) {


output$map <- renderLeaflet({

 p <- input$map_click

 if(is.null(p)){
   leaflet() %>% setView(lng = -43.1729, lat = -22.9068, zoom = 11) %>%
     addTiles(options = providerTileOptions(noWrap = TRUE)) 
 }

 else{
   address <- revgeocode(c(p$lng,p$lat))

   leaflet() %>% setView(lng = p$lng, lat = p$lat, zoom = 16) %>%
   addTiles(options = providerTileOptions(noWrap = TRUE)) %>%
   addCircles(p$lng, p$lat, weight = 1, radius = 100, color =  "black",
                    fillColor = "orange", popup = address, fillOpacity=0.5, opacity=1)
 }


})

output$out <- renderPrint({
validate(need(input$map_click, FALSE))
click <- input$map_click
clat <- click$lat
clng <- click$lng
address <- revgeocode(c(clng,clat))
print(clat)
print(clng)
print(address)

})

})

推荐答案

TL; DR

  1. 创建初始地图,使其 取决于用户输入.
  2. 使用观察者响应用户的点击来更新地图.
  3. 使用leafletProxy更新地图,而无需再次渲染所有内容.
  1. Create the initial map so it is not dependent on user input.
  2. Use an observer that responds to user clicks to update the map.
  3. Use leafletProxy to update the map without rendering everything all over again.

我可以通过制作原始地图并在用户单击位置时使用leafletProxy功能更新地图来做到这一点. Rstudio网站上有一个教程,展示了如何执行此操作.由于不会在每次添加圆时都重新渲染地图,因此有望节省一些计算.

I would do this by making your original map and using the leafletProxy function to update the map when the user clicks on locations. There is a tutorial on the Rstudio site somewhere where they show how to do this. It will hopefully save some computation, since the map won't be re-rendered every time a circle is added.

我还考虑了一些其他事项:将圆数据放入反应性数据集中,并将圆保持在一个组中,从而使您可以通过其他观察者/按钮轻松地隐藏/显示它们.

I also add a couple additional things I would consider: putting the circle data in a reactive dataset, and maintaining the circles in a group, thus allowing you to easily hide/show them with an additional observer/button.

在这里,是一个有效的示例.作为记录,我使用的是github上的传单版本(并建议这样做,因为此软件包正在积极开发中).您可以使用devtools::install_github('rstudio/leaflet')来获得它.我认为CRAN上至少还没有几个新功能,例如可以轻松创建自定义标记.

Here, is a working example. For the record, I'm using the leaflet version from github (and recommend this since this package is under active development). You can get it with devtools::install_github('rstudio/leaflet'). There are at least a couple new features that I don't think are on CRAN yet -- like easily being able to create custom markers.

library(shiny)
library(ggmap)
library(leaflet)

ui <- shinyUI(bootstrapPage(
  leafletOutput("map")
))

server <- shinyServer(function(input, output, session) {
  ## One alternative: store circles data?
  ## I dont actually implement this, but you would do this in the observer as well
  dat <- reactiveValues(circs = data.frame(lng=numeric(), lat=numeric()))

  ## Make your initial map
  output$map <- renderLeaflet({
    leaflet() %>%
      setView(lng = -43.1729, lat = -22.9068, zoom = 11) %>%
        addTiles(options = providerTileOptions(noWrap = TRUE)) 
  })

  ## Observe mouse clicks and add circles
  observeEvent(input$map_click, {
    ## Get the click info like had been doing
    click <- input$map_click
    clat <- click$lat
    clng <- click$lng
    address <- revgeocode(c(clng,clat))

    ## Add the circle to the map proxy
    ## so you dont need to re-render the whole thing
    ## I also give the circles a group, "circles", so you can
    ## then do something like hide all the circles with hideGroup('circles')
    leafletProxy('map') %>% # use the proxy to save computation
      addCircles(lng=clng, lat=clat, group='circles',
                 weight=1, radius=100, color='black', fillColor='orange',
                 popup=address, fillOpacity=0.5, opacity=1)
  })

})

shinyApp(ui=ui, server=server)

结果应该看起来像这样(我已经放大,添加了一些圆圈并激活了一个弹出窗口).

The result should look something like this (I've zoomed in, added some circles and activated a popup).

这篇关于R传单如何在地图上单击并添加圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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