Hi all,
Looking at all the fabulous code that everyone has created for determining the travel time from Google Maps, it inspired me to create my own implementation for my automation, however I wanted to figure out a way to make it not rely on having a predetermined Google Maps direction web address. The idea was to implement it in such a way that it can determine the travel time to any of the appointments in my calender that have location information, which I already fairly religiously include. Sort of like my own implementation of the "when to leave" functionality of Google Now. However, my plan is to make it transportation type aware, which is determined by distance and current/forecast weather conditions. 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 here if you would like to read more on the API.
I just wrote up an 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. I think is an awesome expansion of the capability of the original code.
--EDITED TO INCLUDE THE CODE
- Code: Select all
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 & "&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 5 of the_response
set travelTime to paragraph 9 of the_response
set distance to ("Distance: " & last word of distance)
set travelTime to ("Time: " & last word of travelTime)
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
return the_response
on error
return "Error: CONNECTION_ERROR"
end try
end getDirections
-- Sub-Routine Test
set the_address to "<SOME ADDRESS>"
set the_directions to getDirections(the_address, "walking")
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.
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 or do anything within Indigo.