2

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.

josliber
  • 43,891
  • 12
  • 98
  • 133
areddy
  • 373
  • 3
  • 7
  • 18

2 Answers2

8

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

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • Question tags are meant to capture the contents of the question; please stop adding tags related to your library to question that do not ask about your library. [tag:googleway] only belongs on questions that are specifically about that library. – josliber Jul 01 '16 at 14:04
  • @josliber - I interpreted this meta post - [tagging a question based on its answers](http://meta.stackoverflow.com/q/252079/5977215) to mean it was acceptable to do so. – SymbolixAU Jul 01 '16 at 20:09
  • See for instance http://meta.stackoverflow.com/questions/252079/tagging-a-question-based-on-its-answers#comment6308_252080 on that post -- please only tag a library/tool if it's being asked about in the question. – josliber Jul 01 '16 at 20:23
  • @josliber - is it also policy to declare you are the author of a package if you write it as an answer? If so, should I be flagging answers that violate said policy? – SymbolixAU Jul 02 '16 at 07:25
  • Yes, if you recommend your own package then you need to disclose that you are the author (as you did on this answer). Please do flag answers that violate this policy. – josliber Jul 02 '16 at 14:07
  • @josliber ok - and lastly; if a question explicitely asks about a specific package, then does the author need to identify themselves? I ask because I know a lot of `R` package authors who answer questions without identifying themselves? – SymbolixAU Jul 04 '16 at 02:43
  • 1
    Nobody should be answering package recommendation questions period -- they should be closed because they are asking for package recommendations. Beyond that, the general rule is: if somebody asks about package X, then package X's maintainer can answer without disclosing affiliation (they are not promoting). If somebody asks a question that does not mention package X and package X's maintainer suggests package X to solve it, they need to disclose affiliation and make sure that they are not doing this for a high percentage of their answers. – josliber Jul 04 '16 at 02:51
  • @josliber "... answering package recommendation questions ... " - sorry, that wasn't the intent of my question. However, the rest of your comment answered it nicely :). I'll go away now... – SymbolixAU Jul 04 '16 at 03:05
  • Thank you, though i didn't use your google way, knowing that leaflet can give a less static map compared to ggmap is very helpful – Firhat Nawfan H. Mar 05 '17 at 15:20
  • @SymbolixAU Is it possible to implement the same without specifying the `Origin` and `destination` value?. I would like to implement `add_polylines` to my maps where I will not be having the values of `origin` and `destination` instead I will be having set of `lat` and `Lon`. – Tareva Aug 04 '17 at 10:26
2

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.

Community
  • 1
  • 1
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116