[GIS] How to calculate multiple travel times using gmapsdistance

distancegoogle mapsgoogle-maps-apitimetransit

I am trying to use gmapsdistance on R to calculate multiple transit times from Google Maps.

I have a Google API key and can generate transit travel times for one origin and one destination with the following code:

gmapsdistance("51.7788868858975,-1.48196518842103"
, "51.76732413,-1.233620597" ,"transit",
"googleAPIKey")

However, i have a list of 1000+ origins and destinations in latitude and longitude that i want to find transit times and can only retrieve one at a time. I've tried to adapt the formula provided here to request multiple requests using GGMAP:

google_results <- rbind.fill(apply(subset(TravelExample, select=c("Origin", "Destination")), 1, function(x)
gmapsdistance(x2, x1, "transit", "googleAPIKey")))

I get the following error: Error in gmapsdistance(x2, x1, "transit", "googleAPIkey") :
Google maps is not able to find a route between origin and destination

I would use GGMAPS, but unfortunately you cannot request transit times from Google using the pakagage.

Any ideas how i can request multiple transit times using gmapsdistance?

Best Answer

I would suggest doing it within a loop. Here is a suggestion on how to do it

origin<-c("New+York","Washington+DC")
destination<-c("Philadelphia+PA","Willimington+DE","Harrisburg+PA")
NO<-length(origin)
ND<-length(destination)
time<-matrix(0, nrow = NO, ncol = ND)
for (i in 1:1:NO){
    for(j in 1:1:ND){
        time[i,j]<-gmapsdistance(origin[i],destination[j],mode="driving")$Time
    }
}

I have developed some preliminary code in Python in order to process multiple queries at the same time. You can find the preliminary verison in this link: https://github.com/rodazuero/gmapsdistancePY but you will have to adapt it yourself to your own need. I hope this helps.

Rodrigo

Related Question