【问题标题】:Multiple markers on same coordinate同一坐标上的多个标记
【发布时间】:2016-07-27 22:24:01
【问题描述】:

当从 r 包传单数据中绘制交互式世界地图上的标记时,坐标完全相同。

请看下面的例子:

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

可以使用集群选项显示所有坐标,但这远非我的目标。我不想要集群,并且在完全放大时只显示重叠的标记。当完全放大背景地图时,会变成灰色(“地图数据尚不可用”)。重叠标记的蜘蛛视图是我想要的,但在完全放大时不是。

请看下面的例子:

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

此外,如果还有其他方法可以处理重叠标记,请随时回答。 (例如一个弹出窗口中的多个标记信息)

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    你可以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)

    【讨论】:

    • sf 包有 sf_jitter,这让这更容易!
    【解决方案2】:

    根据我的评论,这里有一个更现代的解决方案(大约 2020 年),它利用了一些旨在让我们的生活更轻松的新软件包(tidyversesf)。我像@TimSalabim 一样使用sf:st_jittermapview。最后,我选择了一个稍微大一点的抖动因子,这样您就不必放大这么远就能看到效果:

    library(mapview)
    library(sf)
    
    Data <- tibble(Name = c("M1", "M2", "M3"),
                   Latitude = c(52L, 52L, 51L), 
                   Longitude = c(50L, 50L, 50L), 
                   Altitude = c(97L, 97L, 108L))
    
    Data %>% 
      st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326) %>% 
      st_jitter(factor = 0.001) %>%
      mapview
    

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 2015-08-02
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      相关资源
      最近更新 更多