【问题标题】:How to add a popup to a polyline that will display whe hovering over a polyline on map for leaflet如何将弹出窗口添加到折线中,当鼠标悬停在地图上的折线上时显示传单
【发布时间】:2021-12-11 07:28:00
【问题描述】:

我在地图上画了一些折线,我想知道是否有办法显示折线的弹出标记。有没有一种方法可以将光标悬停在折线上并显示一个弹出窗口?

enter image description here

【问题讨论】:

  • 嗨@Marc Rich,欢迎来到SO。我想你应该找到你的问题的答案here

标签: r leaflet polyline r-leaflet


【解决方案1】:

要添加仅在悬停时可见的弹出窗口,请使用 leaflet::addPolylines() 调用的 label 参数。您需要使用波浪号 (~) 表示法将 label 参数指向数据对象(或表达式)中的列。

您可以使用labelOptions 来微调外观(我建议方向顶部;noHide = T 将保持标签始终打开)。

library(sf)
library(leaflet)

shape <- st_read(system.file("shape/nc.shp", package="sf")) %>% # included with sf package
  st_transform(4326) # ... or an alarm will ring!

# a polyline, any polyline
ashe <- st_cast(shape[1, ], "MULTILINESTRING") # borders of county Ashe as a line

leaflet(data = ashe) %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addPolylines(color = "red",
               label = ~paste("Here be county ", NAME), # note the tilde / ~
               labelOptions = labelOptions(noHide = F, direction = "top"))

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多