且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单张闪亮的地图标记

更新时间:2023-01-08 16:15:58

我将向您展示一个较小的示例.

I'm going to show you a smaller example of how this works.

注意事项

  1. 单击形状/地图对象将返回latlngid
  2. id值是您使用layerId参数在addMarkers()调用中分配的值
  3. 然后,假设您已使用数据中的id值作为layerId
  4. ,则可以使用此id来过滤数据.
  1. Clicking on a shape / map object will return the lat, lng and id values
  2. The id value is that which you assign inside the addMarkers() call using the layerId argument
  3. You can then use this id to filter your data, assuming you've used an id value from the data as the layerId

示例

在此示例中,我使用的是googleway软件包随附的数据集

Example

In this example I'm using a data set supplied with my googleway package

library(shiny)
library(leaflet) 
library(googleway)

ui <- fluidRow(
  leafletOutput(outputId = "map"),
  tableOutput(outputId = "table")
)

server <- function(input, output){

  ## I'm using data from my googleway package
  df <- googleway::tram_stops

 ## define the layerId as a value from the data
  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addMarkers(data = df, lat = ~stop_lat, lng = ~stop_lon, layerId = ~stop_id)
  })

  ## observing a click will return the `id` you assigned in the `layerId` argument
  observeEvent(input$map_marker_click, {

    click <- input$map_marker_click

    ## filter the data and output into a table
    output$table <- renderTable({
      df[df$stop_id == click$id, ]
    })
  })

}

shinyApp(ui, server)