2017-03-07 54 views
1

我有一个闪亮的应用,用户可以在小工具地图上通过DrawToolbar创建新点。每次设置新标记时,坐标都会添加到data.frame中。我想将这些坐标显示为新添加的标记的弹出窗口。这是可能的,而不会失去拖动或删除新标记的可能性?将弹出框添加到DrawToolbar中的小工具中添加的光标

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

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

data <- data.frame(lat = c(), lon = c()) 

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

    output$map <- renderLeaflet({ 
    leaflet() %>% addTiles() %>% 
     addDrawToolbar(
     targetGroup = "new_points", 
     polylineOptions = FALSE, 
     polygonOptions = FALSE, 
     rectangleOptions = FALSE, 
     circleOptions = FALSE, 
     editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) 
    }) 

    observeEvent(input$map_draw_new_feature, { 
    click_lat <- input$map_draw_new_feature$geometry$coordinates[[2]] 
    click_lon <- input$map_draw_new_feature$geometry$coordinates[[1]] 
    data <- rbind(data, cbind(click_lat, click_lon)) 
    print(data) 
    }) 

} 


shinyApp(ui, server) 

回答

1

可以使用reactiveValues而不是全局变量来存储新的标志物,它会重新绘制地图(将所有标志物弹出),每次添加一个时间:

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

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

server <- function(input, output, session) { 
    data <- reactiveValues(lat = NULL, lon = NULL) 
    output$map <- renderLeaflet({ 
    # Get setView parameters 
    new_zoom <- 2 
    if(!is.null(input$map_zoom)) new_zoom <- input$map_zoom 
    new_lat <- 0 
    if(!is.null(input$map_center$lat)) new_lat <- input$map_center$lat 
    new_lon <- 0 
    if(!is.null(input$map_center$lng)) new_lon <- input$map_center$lng 

    leaflet() %>% addTiles() %>% 
     setView(new_lon,new_lat,zoom = new_zoom) %>% 
     addDrawToolbar(
     targetGroup = "new_points", 
     polylineOptions = FALSE, 
     polygonOptions = FALSE, 
     rectangleOptions = FALSE, 
     circleOptions = FALSE, 
     editOptions = editToolbarOptions(
      selectedPathOptions = selectedPathOptions()))-> map 

    if (!is.null(data$lat)) { 
     addMarkers(map, lng=data$lon, lat=data$lat, 
      popup=paste("lat=", data$lat, ", lon=", data$lon))} 
    else map 
    }) 

    observeEvent(input$map_draw_new_feature, { 
    click_lat <- input$map_draw_new_feature$geometry$coordinates[[2]] 
    click_lon <- input$map_draw_new_feature$geometry$coordinates[[1]] 
    data$lat <- c(data$lat,click_lat) 
    data$lon <- c(data$lon,click_lon) 
    }) 
} 

shinyApp(ui, server) 

通知的setView呼叫以避免缩放到您添加的第一个标记

+0

有没有办法做到这一点,而不会失去拖动或删除标记的可能性?在我的情况下,弹出窗口应该有助于选择哪些标记添加不正确,因此可以拖动或删除这些标记。我还会更新我的问题以清楚说明。 – needRhelp