Marker mouse click event in R leaflet for shiny

白昼怎懂夜的黑 提交于 2019-11-27 03:57:47

问题


How can I receive a mouse click event on a marker in a leaflet map in R? I'm using the RStudio/leaflet and running through Shiny.

I'd like to get the value of a marker (e.g., ID) and use that to update a sidebarPanel.


回答1:


You want to use input$MAPID_marker_click. See the example below.

library(shiny)
library(leaflet)

latitude <- c(35.94077, 35.83770, 35.84545, 35.81584, 35.79387, 36.05600)
longitude <- c(-78.58010, -78.78084, -78.72444, -78.62568, -78.64262, -78.67600)
radius<-c(15, 12, 12, 12, 12, 15)
ids<-c("a", "b", "c", "d", "e", "f")

shinyApp(
  ui = fluidPage(
    fluidRow(
      leafletMap(
        "map", "100%", 400,
        initialTileLayer = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
        initialTileLayerAttribution = HTML('Maps by <a href="http://www.mapbox.com/">Mapbox</a>'),
        options=list(
          center = c(37.45, -93.85),
          zoom = 4,
          maxBounds = list(list(17, -180), list(59, 180))))),
    fluidRow(verbatimTextOutput("Click_text"))),
  server = function(input, output, session){
        map = createLeafletMap(session, 'map')
        session$onFlushed(once=T, function(){

          map$addCircleMarker(lat = latitude, 
                              lng = longitude, 
                              radius = radius, 
                              layerId=ids)
        })        

        observe({
          click<-input$map_marker_click
          if(is.null(click))
            return()
          text<-paste("Lattitude ", click$lat, "Longtitude ", click$lng)
          text2<-paste("You've selected point ", click$id)
          map$clearPopups()
          map$showPopup( click$lat, click$lng, text)
          output$Click_text<-renderText({
            text2
          })

        })

  }
)

The argument layerId in the addCircleMarker function passes the ids to the click$id.



来源:https://stackoverflow.com/questions/28938642/marker-mouse-click-event-in-r-leaflet-for-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!