简体   繁体   English

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

[英]Connecting Spatial points with directed lines using PlotGoogleMaps package in R

I have been trying to do some dynamic Google Map plots from R using the PlotGoogleMaps package. 我一直在尝试使用PlotGoogleMaps包从R做一些动态的Google Map图。 I have no problem in plotting the Spatial Points on google maps using this package. 使用此软件包在谷歌地图上绘制空间点是没有问题的。

I want to connect some pairs of the spatial points plotted with lines. 我想连接一些用线条绘制的空间点对。 I could not find any way to do that in R with the package. 我在包里找不到任何方法在R中做到这一点。 It seems that Google Maps has a function for that in the API: google.maps.Polyline . Google地图似乎在API中具有此功能: google.maps.Polyline

Could anyone help with how this can be done in R? 任何人都可以帮助在R中如何做到这一点? I referred to links like How to get image iconMarker working for plotGoogleMaps R? 我提到了如何获取图像iconMarker为plotGoogleMaps R工作的链接? to make sure that my basic plot is working fine. 确保我的基本情节工作正常。 How can I join them to show a path? 我如何加入他们以显示路径?

I ran the code below 我运行下面的代码

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

It works fine. 它工作正常。 But I do not know how to connect the plotted points to make a path between a selected pair of points on the map. 但我不知道如何连接绘制的点以在地图上的一对选定点之间建立路径。

This doesn't directly answer your question, but I'm posting it as a potential alternative. 这并没有直接回答你的问题,但我将其作为潜在的替代方案发布。

I've built the googleway package, which includes a Google Maps widget. 我已经构建了googleway软件包,其中包含一个Google Maps小部件。

To use Google Maps you need a Google Maps API key 要使用Google地图,您需要使用Google Maps API密钥

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM