当从具有完全相同坐标的 r 包传单数据中绘制交互式 worlmap 上的标记时,它们将相互重叠。
请参阅下面的示例:
library(leaflet)
Data <- structure(list(Name = structure(1:3, .Label = c("M1", "M2", "M3"), class = "factor"), Latitude = c(52L, 52L, 51L), Longitude = c(50L, 50L, 50L), Altitude = c(97L, 97L, 108L)), .Names = c("Name", "Latitude", "Longitude", "Altitude"), class = "data.frame", row.names = c(NA, -3L))
leaflet(data = Data) %>%
addProviderTiles("Esri.WorldImagery", options = providerTileOptions(noWrap = TRUE)) %>%
addMarkers(~Longitude, ~Latitude, popup = ~as.character(paste(sep = "",
"<b>",Name,"</b>","<br/>", "Altitude: ",Altitude)))
可以使用 cluster 选项显示所有坐标,但这与我的目标相去甚远。我不想要集群,并且在完全放大时只显示重叠的标记。当完全放大时,背景 map 变成灰色(“ map 数据尚不可用”)。重叠标记的蜘蛛 View 是我想要的,但在完全放大时不是。
请参阅下面的示例:
leaflet(data = Data) %>%
addProviderTiles("Esri.WorldImagery", options = providerTileOptions(noWrap = TRUE)) %>%
addMarkers(~Longitude, ~Latitude, popup = ~as.character(paste(sep = "",
"<b>",Name,"</b>","<br/>", "Altitude: ",Altitude)), clusterOptions = markerClusterOptions())
我找到了一些关于我想要的解决方案的文献,但我不知道如何在 r 传单代码/包中实现它。
https://github.com/jawj/OverlappingMarkerSpiderfier-Leaflet
此外,如果有其他方法来处理重叠标记,请随时回答。 (例如一个弹出窗口中的多个标记信息)
最佳答案
您可以 jitter()
你的坐标略:
library(mapview)
library(sp)
Data <- structure(list(Name = structure(1:3, .Label = c("M1", "M2", "M3"),
class = "factor"),
Latitude = c(52L, 52L, 51L),
Longitude = c(50L, 50L, 50L),
Altitude = c(97L, 97L, 108L)),
.Names = c("Name", "Latitude", "Longitude", "Altitude"),
class = "data.frame", row.names = c(NA, -3L))
Data$lat <- jitter(Data$Latitude, factor = 0.0001)
Data$lon <- jitter(Data$Longitude, factor = 0.0001)
coordinates(Data) <- ~ lon + lat
proj4string(Data) <- "+init=epsg:4326"
mapview(Data)
这样你仍然需要放大标记来分开,你需要放大多远取决于
factor
jitter()
中的属性.请注意,我正在使用
library(mapview)
在示例中为简单起见。
关于r - 同一坐标上的多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469379/