Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display google map directions given latitude and longitude information in r

I have with me the latitude and logitude information for few locations. Here is a sample:

lat<-c(17.48693,17.49222,17.51965,17.49359,17.49284,17.47077)
long<-c(78.38945,78.39643,78.37835,78.40079,78.40686,78.35874)

I want to plot these locations in some order(say lat-long combination of first elements in the above vectors will be the starting point and i need to travel in the same order till last location) with google map directions in R. Upon some search I found that there is a google map api from which i can get google map screenshot of specified locations and on top of it we need to plot lines to connect them. But what I need is google map driving directions to connect the locations (not ggplot lines). Please help.

like image 278
areddy Avatar asked Nov 27 '25 14:11

areddy


1 Answers

I've written the package googleway to access google maps API with a valid API key.

You can use the function google_directions() to get the directions, including waypoints, route steps, legs, distances, times, etc.

For example

library(googleway)

## using a valid Google Maps API key
key <- "your_api_key"

## Using the first and last coordinates as the origin/destination
origin <- c(17.48693, 78.38945)
destination <- c(17.47077, 78.35874)

## and the coordinates in between as waypoints
waypoints <- list(via = c(17.49222, 78.39643),
                  via = c(17.51965, 78.37835),
                  via = c(17.49359, 78.40079),
                  via = c(17.49284, 78.40686))
## use 'stop' in place of 'via' for stopovers

## get the directions from Google Maps API
res <- google_directions(origin = origin,
                         destination = destination,
                         waypoints = waypoints,
                         key = key)  ## include simplify = F to return data as JSON

The result is all the data received from Google Maps

## see the structure
# str(res)

The line that you see on Google Maps is contained in

res$routes$overview_polyline$points
# [1] "slviBqmm}MSLiA{B^wAj@sB}Ac@...

Which is an encoded polyline.

To get the lat/lon from this use the function decode_pl()

df_polyline <- decode_pl(res$routes$overview_polyline$points)
head(df_polyline)
#        lat      lon
# 1 17.48698 78.38953
# 2 17.48708 78.38946
# 3 17.48745 78.39008
# 4 17.48729 78.39052
# 5 17.48707 78.39110
# 6 17.48754 78.39128

Which of course you can then plot as you wish

library(leaflet)

leaflet() %>%
  addTiles() %>%
  addPolylines(data = df_polyline, lat = ~lat, lng = ~lon)

enter image description here


Edit 2017-07-21

As of googleway 2.0 you can plot the polyline in a Google Map, either using the decoded coordinates as before, or by using the polyline directly

google_map(key = key) %>%
    add_polylines(data = data.frame(polyline = res$routes$overview_polyline$points), 
                                polyline = "polyline")

enter image description here

like image 134
SymbolixAU Avatar answered Nov 30 '25 04:11

SymbolixAU



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!