3
votes

I have been trying to do some dynamic Google Map plots from R using the PlotGoogleMaps package. 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. It seems that Google Maps has a function for that in the API: google.maps.Polyline.

Could anyone help with how this can be done in R? I referred to links like How to get image iconMarker working for 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.

1
Please improve your question. Start here: How to create a Minimal, Complete, and Verifiable example or otherwise show what have you done so far.iled
I ran the below fine (taken from link in my original question)user7876297
Your question should be self contained.iled

1 Answers

0
votes

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.

To use Google Maps you need a 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