3

I try to use Google Maps and R to calculate travel times per transit between an origin an destination.

The guidelines for the search can be found at: https://developers.google.com/maps/documentation/directions/#TravelModes

When I submit the latitude and the longitude of the origin and destination as literals, things work fine.

For instance, the following R code executes correctly and we obtain the distance and trip duration (the output of the search is in JSON format and is converted to an R object with fromJSON)

library(rjson)
library(gooJSON)
route <- url('http://maps.googleapis.com/maps/api/directions/json?                  origin=51.13854,4.384575&destination=51.13156,4.387118&region=be&sensor=false&mode=transit&departure_time=1372665319')
route_file  <- file("route_file.json")
L <- readLines(route,-1)
writeLines(L, route_file)
close(route)
routesR_zone1_to_zone20 <- fromJSON( file = route_file )
routesR_zone1_to_zone20$routes[[1]][[3]][[1]]$distance$value/1000
routesR_zone1_to_zone20$routes[[1]][[3]][[1]]$duration$value/60

However, what I am really interested in is to repeat this operation for thousands of origin-destination pairs. The longitude and the latitude of the origins and destinations then become variables.

For instance:

> lat_or
[1] 51.13854
> long_or
[1] 4.384575
> lat_des
[1] 51.13156
> long_des
[1] 4.387118
> route <- url('http://maps.googleapis.com/maps/api/directions/json?        origin=lat_or,long_or&destination=lat_des,long_des&region=be&sensor=false&mode=transit&departure_time=1372665319')
> route_file  <- file("route_file.json")
> L <- readLines(route,-1)
> writeLines(L, route_file)
> close(route)
> routesR_zone1_to_zone20 <- fromJSON( file = route_file )
> routesR_zone1_to_zone20
$routes
list()

$status
[1] "NOT_FOUND"

Thus, although the coordinates are the same as in the previous example, this time, no route is found.

I suppose that the problem is that, when the url is accessed, lat_or etc are not "translated" in the corresponding numeric values, and that Google tries to calculate the route between the literals " lat_or,long_or" and " lat_des,long_des".

Does anyone have a suggestion on how to circumvent the problem?

Laurent Franckx
  • 161
  • 1
  • 6
  • See `ggmap` package. Otherwise see [my answer here].(http://stackoverflow.com/questions/17282124/how-do-i-get-driving-time-from-google-maps-api/17282224#17282224) – agstudy Jul 02 '13 at 14:46

1 Answers1

0

Standard text processing:

 lat_or <- 51.13854
 long_or <- 4.384575
 lat_des <-  51.13156
 long_des <- 4.387118
 route <- url(paste0("http://maps.googleapis.com/maps/api/directions/json?origin=",lat_or,",",long_or,"&destination=",lat_des,",",long_des,"&region=be&sensor=false&mode=transit&departure_time=1372665319") )

 route_file  <- file("route_file.json")
 L <- readLines(route,-1)
 writeLines(L, route_file)
 close(route)
 routesR_zone1_to_zone20 <- fromJSON( file = route_file )
 routesR_zone1_to_zone20$routes[[1]][[3]][[1]]$distance$value/1000
#[1] 1.161
 routesR_zone1_to_zone20$routes[[1]][[3]][[1]]$duration$value/60
#[1] 11.73333
IRTFM
  • 258,963
  • 21
  • 364
  • 487