我有 R 中交互式地图的代码:
library(leaflet)
library(inlmisc)
Long = rnorm(1000, -71, 0.5)
Lat = rnorm(1000, 42.3, 0.5)
loc = rep("loc", 1000)
Name = rep("Location", 1000)
num = 1:1000
Label = paste0(loc, "_", num)
Location = paste0(Name, "_", num)
df = data.frame(Name, Lat, Long, Label)
map <- leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
addMarkers( clusterOptions = markerClusterOptions(), popup = ~paste("title: ", Name)) %>%
addTiles() %>%
setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
addMarkers(~Long, ~Lat, popup = ~Name, group="marker", label = ~Label) %>%
inlmisc::AddSearchButton(group = "marker", zoom = 15,
textPlaceholder = "Search here")
地图似乎工作正常 - 但“图标”显示两次(即蓝色图钉和彩色圆圈,例如黄色、绿色):
有没有办法让当你缩小时,蓝色图钉折叠成彩色圆圈 - 当你放大时,彩色圆圈折叠成蓝色图钉?
看起来您添加了两次标记。
第一行
addMarkers
,进行缩放聚类,听起来你感兴趣。
您可以将
group
和 label
选项添加到第一个 addMarkers
调用,以便能够搜索并具有鼠标悬停标签以及聚类。
map <- leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
addMarkers( clusterOptions = markerClusterOptions(),
popup = ~paste("title: ", Name),
group="marker", label = ~Label) %>%
addTiles() %>%
setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
inlmisc::AddSearchButton(group = "marker", zoom = 15,
textPlaceholder = "Search here")
使用@Senithil913的答案 - 我添加了一个重置选项:
map <- leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
addMarkers( clusterOptions = markerClusterOptions(),
popup = ~paste("title: ", Name),
group="marker", label = ~Label) %>%
addTiles() %>%
setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
inlmisc::AddSearchButton(group = "marker", zoom = 15,
textPlaceholder = "Search here") %>% addResetMapButton()