r - 使用 R 中的 PlotGoogleMaps 包将空间点与定向线连接起来

标签 r plotgooglemaps

我一直在尝试使用 PlotGoogleMaps 包从 R 中绘制一些动态 Google map 。使用这个包在谷歌地图上绘制空间点没有问题。

我想连接一些用线绘制的空间点对。我找不到任何方法在 R 中使用包来做到这一点。似乎 Google map 在 API 中有一个函数:google.maps.Polyline

任何人都可以帮助解决如何在 R 中完成的问题吗?我提到了像 How to get image iconMarker working for plotGoogleMaps R? 这样的链接确保我的基本情节工作正常。我怎样才能加入他们来显示路径?

我运行了下面的代码

library(plotGoogleMaps)
vessels = data.frame(id = c(1:10)
                     , lat = c(22.0959, 22.5684, 21.9189, 21.8409, 22.4663, 22.7434, 22.1658, 24.5691, 22.4787, 22.3039)
                     , lon = c(114.021, 114.252, 113.210, 113.128, 113.894, 114.613, 113.803, 119.730, 113.910, 114.147))
group1 = vessels[1:5,]
group2 = vessels[6:10,]

coordinates(group1) = ~ lon + lat
proj4string(group1) = CRS("+proj=longlat +datum=WGS84")
group1 <- SpatialPointsDataFrame( group1 , data = data.frame( ID = row.names( group1 ) ))

coordinates(group2) = ~ lon + lat
proj4string(group2) = CRS("+proj=longlat +datum=WGS84")
group2 <- SpatialPointsDataFrame( group2 , data = data.frame( ID = row.names( group1 ) ))
m <- plotGoogleMaps(group1, legend = FALSE, layerName = "Vessels 1"
                    , add =T,
                    iconMarker=rep('http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png',nrow(group1) ), 
                    mapTypeId='ROADMAP', filename = "out.htm")

m <- plotGoogleMaps(group2,legend = FALSE, layerName = "Vessels 2"
                    , previousMap = m , add = F
                    , iconMarker = rep('http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png',nrow(group2) )
                    , filename = "out.htm")

它工作正常。但我不知道如何连接标绘点以在 map 上选定的一对点之间建立路径。

最佳答案

这并没有直接回答您的问题,但我将其发布为一个潜在的替代方案。

我构建了 googleway包,其中包括一个 Google map 小部件。

要使用 Google map ,您需要一个 Google Maps API key

library(googleway)

vessels = data.frame(id = c(1:10)
                    , lat = c(22.0959, 22.5684, 21.9189, 21.8409, 22.4663, 22.7434, 22.1658, 24.5691, 22.4787, 22.3039)
                    , lon = c(114.021, 114.252, 113.210, 113.128, 113.894, 114.613, 113.803, 119.730, 113.910, 114.147))

vessels$group <- c(rep(1, 5), rep(2, 5))

## a google maps api key
map_key <- "your_api_key"

google_map(key = map_key, data = vessels) %>%
    add_circles(radius = 1000) %>%
    add_polylines(lat = 'lat', lon = 'lon', id = 'group', 
                   mouse_over_group = 'group')

enter image description here

关于r - 使用 R 中的 PlotGoogleMaps 包将空间点与定向线连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443648/

相关文章:

r - 如何找到具有相同值的两行,但以相反的顺序删除其中一行?在R中

r - Centos 配置上的 Shiny 服务器

r - Shiny 的服务器安装 : Server not responding on port 3838

r - 如何在R中获得矩阵的正确特征向量?

r - 强制 R 输出为最多两位小数的科学记数法

r - 如何让图像 iconMarker 适用于plotGoogleMaps R?

r - 如何从 R bubbleGoogleMaps 获取比例气泡大小?