Question

Extract travel time from Google Maps Direction, for multiple searches

  • 27 February 2023
  • 2 replies
  • 37 views

Badge +12

I've read the documentation on how to connect to Google Maps with an origin and a destination parameter; https://developers.google.com/maps/documentation/urls/get-started

 

Now, let's assume that I have an Excel sheet with columns for origins and destinations, for maybe 100 posts. Is it possible to use FME to send these to Google Maps and then get each of the calculated travel times back, to create a "travel time" column for the excel sheet?


2 replies

Badge

You can achieve this with an HTTPCaller.

 

Input the features from your Excel file with attributes 'origins and 'destinations' into an HTTP Caller.

 

The Request URL can be constructed like below, where the origin and destination coordinates are being pulled from the respective attributes in the Excel sheet.

 

(I think the DistanceMatrix API is the one you'll need, but I could be wrong)

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=@Value(origins)&destinations=@Value(destinations)&departure_time=now&key=<API_KEY>

 

(add/remove/edit API parameters according to your needs)

 

Set the HTTP Method to 'GET'.

The HTTPCaller will send one request to Google API per record in the Excel sheet.

 

The response is returned as a JSON string in the default attribute "_response_body". You can then use something like the JSONExtractor transformer to pull out the travel time.

 

I would also read up on Google's Terms of Services to see if they allow such extraction and storage of data. HERE's API (free) is a good alternative to Google API for such purposes.

 

EXTRA: If some HTTP calls are not going through and are being rejected for making calls to the API too quickly, it is good practice to put these rejected calls through the Decelerator transformer, and back into a duplicate HTTPCaller.

Badge +12

Thank you @maaz​ 

That would probably work, though I was hoping to just use the regular Maps URL at Google Maps, not the distance matrix API which requires accounts, API keys and the likes.

 

I do get a result with the HTTPCaller and my origin/destination parameters to Google Maps URL as well, it is just that it returns an html file and not a JSON, so it is very difficult to find and extract the total travel time in that mass of html text.

Reply