【问题标题】:Replace marker when button clicked单击按钮时替换标记
【发布时间】:2021-11-12 09:08:08
【问题描述】:

我正在启动一个带有传单地图的 Shiny 应用程序。

我需要用户能够在地图上放置两个单独的标记(起点和终点),并可能在以后替换它们。

所以我所做的是创建一个起点按钮和一个目标按钮,这样当用户点击其中一个按钮时,他们会在下次点击地图时放置或更新相应的标记。

library(shiny)
library(shinyWidgets)
library(leaflet)
library(RColorBrewer)
library(osrm)

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
                actionButton("change_orig", "Origine"),
                actionButton("change_dest", "Destination"),
  )
)

server <- function(input, output, session) {
  
  origin_icon = makeIcon("https://static.thenounproject.com/png/1477944-200.png",
                         iconWidth=24, iconHeight=30)
  destination_icon = makeIcon("https://static.thenounproject.com/png/924206-200.png",
                              iconWidth=24, iconHeight=30)
  
  output$map <- renderLeaflet({
    leaflet() %>% addTiles(providers$OpenStreetMap) %>%
      addProviderTiles(providers$OpenStreetMap, group="Open Street Map") %>% 
      addProviderTiles(providers$OpenTopoMap, group="Open Topo Map") %>% 
      fitBounds(2.907730, 45.856550, 3.310954, 45.714034) %>%
      addLayersControl(
        baseGroups = c("Open Street Map", "Open Topo Map")
      )
    
  })
  
  # When "Origin" is selected, 
  # a click on the map will (re)place the origin marker.
  observeEvent(input$change_orig, {
    observeEvent(input$map_click, {
      click = input$map_click
      leafletProxy('map')%>%
        clearGroup("origin") %>%
        addMarkers(lng=click$lng, lat=click$lat, icon=origin_icon,
                   group="origin")
      })
  })
  
  # When "Destination" is selected, 
  # a click on the map will (re)place the destination marker.
  observeEvent(input$change_dest, {
    observeEvent(input$map_click, {
      click = input$map_click
      leafletProxy('map') %>%
        clearGroup("destination") %>%
        addMarkers(lng=click$lng, lat=click$lat, icon=destination_icon,
                   group="destination")
    })
  })
  
  
}

shinyApp(ui, server)

我在这里面临几个问题。

  • 如果我在单击其中一个按钮之前已经单击了地图,则应用程序将考虑上一次单击并在此处添加标记。
  • 最重要的是:即使我在不​​同的组中创建起始标记和目的地标记,clearGroup 也会删除目的地和起始标记并在同一位置重新创建它们。

如何确保只创建或替换一个标记而不影响另一个?

我怎么能只在按钮被选中后才监听点击事件呢?

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    所以我最终选择了一个radioButtons,而不是两个actionButtons。

    然后在observeEvent for map_click 中,我使用了一个简单的if 来检查选择了哪个单选按钮:

    library(shiny)
    library(shinyWidgets)
    library(leaflet)
    library(RColorBrewer)
    library(osrm)
    
    ui <- bootstrapPage(
      tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
      leafletOutput("map", width = "100%", height = "100%"),
      absolutePanel(top = 10, right = 10,
                    radioButtons(
                      'orig_dest_switch',
                      "Changer l'origine ou la destination",
                      choices = c("Origine", "Destination"),
                      choiceValues = c("orig", "dest"),
                      inline = TRUE),
      )
    )
    
    server <- function(input, output, session) {
      
      origin_icon = makeIcon("https://static.thenounproject.com/png/1477944-200.png",
                             iconWidth=24, iconHeight=30)
      destination_icon = makeIcon("https://static.thenounproject.com/png/924206-200.png",
                                  iconWidth=24, iconHeight=30)
      
      
      output$map <- renderLeaflet({
        leaflet() %>% addTiles(providers$OpenStreetMap) %>%
          addProviderTiles(providers$OpenStreetMap, group="Open Street Map") %>% 
          addProviderTiles(providers$OpenTopoMap, group="Open Topo Map") %>% 
          fitBounds(2.907730, 45.856550, 3.310954, 45.714034) %>%
          addLayersControl(
            baseGroups = c("Open Street Map", "Open Topo Map")
          )
        
      })
      
      observeEvent(input$map_click, {
        click = input$map_click
        # Vérifier si l'on update le marker d'origine ou de destination
        if(input$orig_dest_switch=="Origine"){
          origin = list(lng=click$lng, lat=click$lat)
          leafletProxy('map')%>%
            clearGroup("origin") %>%
            addMarkers(lng=click$lng, lat=click$lat, icon=origin_icon,
                       group="origin")
        } else {
          destination = list(lng=click$lng, lat=click$lat)
          leafletProxy('map')%>%
            clearGroup("destination") %>%
            addMarkers(lng=click$lng, lat=click$lat, icon=destination_icon,
                       group="destination")
        }
      })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多