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®ion=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®ion=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?