2016-04-07 70 views
7

當從包裹傳單的交互式世界地圖繪製標記時,具有完全相同座標的數據將彼此重疊。同一座標上的多個標記

請參見下面的例子:

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))) 

有一個possibilty顯示所有與集羣選項協調,但這遠遠不是我的目標。當完全放大時,我不想顯示羣集,只顯示重疊的標記。完全放大後,背景地圖變爲灰色(「地圖數據尚未提供」)。重疊標記的蜘蛛觀點是我想要的,而不是當在完全放大

參見下面的例子:

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()) 

我發現有關我想解決一些literatur,但我不知道如何實現它在r傳單代碼/包中。 https://github.com/jawj/OverlappingMarkerSpiderfier-Leaflet

此外,如果還有其他方法來處理重疊標記,隨時回答。 (例如,在一個彈出多個標記信息)

回答

9

可以略微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) 

這樣你仍然需要放大的標誌物分開,你需要多遠放大依賴jitter()中的factor屬性。

請注意,爲簡單起見,我在示例中使用了library(mapview)