使用R中的PlotGoogleMaps包将空间点与有向线连接

时间:2017-04-17 00:40:21

标签: r plotgooglemaps

我一直在尝试使用PlotGoogleMaps包从R做一些动态的Google Map图。使用此软件包在Google地图上绘制空间点时没有问题。

我想连接一些用线条绘制的空间点对。我在包里找不到任何方法在R中做到这一点。 Google地图似乎在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")

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

1 个答案:

答案 0 :(得分:0)

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

我构建了googleway包,其中包含Google地图小部件。

要使用Google地图,您需要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