As a reminder, there are usage restrictions on the Directions API. You can read the terms of use here.
You can take a copy of this sheet and the code here
Page Content
hide
Example
Let’s say we have a list of destinations and one start point, and want to sort the destinations in order of distance from the start point, and return coordinate, distance and driving time to each of these destinations. Using the functions in Minimizing maps directionfinder api calls, this becomes fairly trivial
function tryThis () { var result = routeDistanceWaypointsFetch( ['New york','Los Angeles',"San Francisco","vancouver","chicago"], ['seattle'], {mode:'driving',apiKey:'', useMiles:true, maxWayPoints:8} ); Logger.log( JSON.stringify( Object.keys(result).map ( function(p) { return result[p]; }) .sort (function(a,b) { return (a.distance - b.distance) ; }) .map (function(p) { if (p.swapped) { return { from : { place:p.a, coords:p.start}, to: { place:p.b, coords:p.end}, distance: p.distance, duration: p.duration } } else { return { from : { place:p.b, coords:p.end}, to: { place:p.a, coords:p.start}, distance: p.distance, duration: p.duration } } }) )); }
Gives this result
[ { "from": { "place": "seattle", "coords": { "lat": 47.6063683, "lng": -122.3322218 } }, "to": { "place": "vancouver", "coords": { "lat": 49.2611415, "lng": -123.1139293 } }, "distance": 140.98601666268988, "duration": 8762 }, { "from": { "place": "seattle", "coords": { "lat": 47.6063683, "lng": -122.3322218 } }, "to": { "place": "San Francisco", "coords": { "lat": 37.7749901, "lng": -122.4194926 } }, "distance": 807.3165215143562, "duration": 44112 }, { "from": { "place": "seattle", "coords": { "lat": 47.6063683, "lng": -122.3322218 } }, "to": { "place": "Los Angeles", "coords": { "lat": 34.0523633, "lng": -118.2435601 } }, "distance": 1135.1109520400857, "duration": 60554 }, { "from": { "place": "seattle", "coords": { "lat": 47.6063683, "lng": -122.3322218 } }, "to": { "place": "chicago", "coords": { "lat": 41.8781139, "lng": -87.6297872 } }, "distance": 2063.609768949336, "duration": 107174 }, { "from": { "place": "seattle", "coords": { "lat": 47.6063683, "lng": -122.3322218 } }, "to": { "place": "New york", "coords": { "lat": 40.70458, "lng": -73.97832799999999 } }, "distance": 2854.445040960789, "duration": 150365 } ]
For help and more information join our forum, follow the blog, follow me on Twitter