Traffic info from Google API

I found this travel time from Google Maps on an Indigo automation forum, and edited it in to an applescript. The great news on making this work is that there is actually an open Google API for this functionality called the “Directions API” which has a very reasonable free limit of 2,500 calls per day, click https://developers.google.com/maps/documentation/directions/ if you would like to read more on the API.

AppleScript sub-routine to output the distance and travel time for a given address or lat./long. that utilizes the API with error handling and parsing.

Here is a website http://itouchmap.com/latlong.html to find the Latitude and Longitude of a point.


on getDirections(destination, transportationType)
   set googleAPIkey to "<YOUR GOOGLE API KEY GOES HERE>" as string -- Enter your Google Directions API key here
   set origin to "<YOUR HOME LAT,LONG GOES HERE>" as string -- Enter your home's latitude and longitude here in the form of (-)##.######,(-)##.######

try
		-- Format the Address into a form that the Google Directions API can process
		set AppleScript's text item delimiters to the " "
		set destination to every text item of destination
		set AppleScript's text item delimiters to the "+"
		set destination to destination as string
		set AppleScript's text item delimiters to ""
		log destination
		
		-- Query Google Servers for the direction information
		set the_response to do shell script "curl -s -m 10 'https://maps.googleapis.com/maps/api/directions/json?origin=" & origin & "&destination=" & destination & "&departure_time=now&mode=" & transportationType & "&key=" & googleAPIkey & "&sensor=false'"
		
		set AppleScript's text item delimiters to "\"status\" : \""
		set directionsStatus to text item 2 of the_response
		set AppleScript's text item delimiters to "\""
		set directionsStatus to text item 1 of directionsStatus
		set AppleScript's text item delimiters to ""
		
		-- Determine if the Google Directions API was successful and if so, parse out the distance and travel time from the origin to the destination
		if directionsStatus is "OK" then
			set AppleScript's text item delimiters to "\"legs\" : ["
			set the_response to text item 2 of the_response
			set AppleScript's text item delimiters to "\"steps\""
			set the_response to text item 1 of the_response
			set AppleScript's text item delimiters to ""
			set distance to paragraph 4 of the_response
			set travelTime to paragraph 8 of the_response
			set distance to ("Distance to work: " & second word of distance) & " miles."
			set travelTime to ("Your estimated travel time: " & second word of travelTime) & " minutes.
			set AppleScript's text item delimiters to ""
			
			-- Return the values in multi-line text
			set the_response to (distance & return & travelTime)
		else
			set the_response to ("Error: " & directionsStatus)
		end if
		say the_response
	on error
		return "Error: CONNECTION_ERROR"
	end try
end getDirections

-- Sub-Routine Test
set the_address to "<YOUR DESTINATION LAT,LONG GOES HERE>" -- Enter latitude and longitude here in the form of (-)##.######,(-)##.######
set the_directions to getDirections(the_address, "driving")
#log the_directions

It is important to note that to use the service you will need to click here to access your Google API Console from where you can activate the “Directions API” on your Google account under the “APIs & auth” tab and grab your API key to copy into the AppleScript Sub-Routine code block.

The downside of this is no updated “duration_in_traffic” since that feature is reserved for Google Business users. So the time of travel never changes due to changing traffic conditions. :frowning:

Also be aware that all this does currently is pull the data and parse out the information, it must be integrated into your own code to access any of the addresses in your calendar.

1 Like