【发布时间】:2021-12-11 07:28:00
【问题描述】:
我在地图上画了一些折线,我想知道是否有办法显示折线的弹出标记。有没有一种方法可以将光标悬停在折线上并显示一个弹出窗口?
【问题讨论】:
-
嗨@Marc Rich,欢迎来到SO。我想你应该找到你的问题的答案here
标签: r leaflet polyline r-leaflet
我在地图上画了一些折线,我想知道是否有办法显示折线的弹出标记。有没有一种方法可以将光标悬停在折线上并显示一个弹出窗口?
【问题讨论】:
标签: r leaflet polyline r-leaflet
要添加仅在悬停时可见的弹出窗口,请使用 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"))
【讨论】: