Last active
May 6, 2020 14:48
-
-
Save avielsh/7395002293cd25f016b3ef10af1c5f84 to your computer and use it in GitHub Desktop.
Return distance and duration between two addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get Distance between 2 different addresses. | |
* @param start_address Address as string Ex. "בורגשוב 35, תל אביב" | |
* @param end_address Address as string Ex. "הקישון 140,תל אביב" | |
* @param ret Return type as string Ex. "km" or "duration". default is both | |
* @customfunction | |
*/ | |
function GOOGLEMAPS(start_address,end_address,ret) { | |
// start_address = "Tel aviv"; | |
// end_address = "Haifa"; | |
var mapObj = Maps.newDirectionFinder(); | |
mapObj.setOrigin(start_address) ; | |
mapObj.setDestination(end_address) ; | |
var directions = mapObj.getDirections(); | |
// Logger.log(directions["routes"][0]["legs"][0]["distance"]["value"]); | |
var meters = directions["routes"][0]["legs"][0]["distance"]["value"]; | |
var km = (meters /1000).toFixed(2); | |
var duration = directions["routes"][0]["legs"][0]["duration"]["value"]; | |
var minutes = Math.round(duration / 60); | |
switch(ret) { | |
case "km": | |
return km; | |
break; | |
case "duration": | |
return minutes; | |
break; | |
default: | |
return km + " km in " + minutes + " minutes" | |
} | |
//Logger.log(duration) ; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment