areddy
areddy

Reputation: 383

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.

Upvotes: 2

Views: 5061

Answers (2)

SymbolixAU
SymbolixAU

Reputation: 26258

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

Upvotes: 8

JasonAizkalns
JasonAizkalns

Reputation: 20483

This basically comes down to creating a route_df and then plotting the results as geom_path. For example, for a single route, you could do something like this:

library(ggmap)

route_df <- route(from = "Hyderabad, Telangana 500085, India",
                  to = "Kukatpally, Hyderabad, Telangana 500072, India",
                  structure = "route")

my_map <- get_map("Hyderabad, Telangana 500085, India", zoom = 13)

ggmap(my_map) +
  geom_path(aes(x = lon, y = lat), color = "red", size = 1.5,
            data = route_df, lineend = "round")

Map with route

So you could probably approach this by generating each of the from-to routes and rbind-ing all of the results into one large route_df and plotting the final results. It's easier for others to help you if you make an attempt and show where (with code) you are getting stuck. You may want to edit your original question or possibly submit a new one after you show what you have tried.

This SO post with this answer should prove helpful.

Upvotes: 2

Related Questions