Direction minimizer - other usages

In Minimizing maps directionfinder api calls I showed how to make the most of your directions API quota in the context of a spreadsheet. Here's an example of calling it directly, say when you are working on a map viz and need to work on some directions and geocoding.
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 at https://docs.google.com/spreadsheet/ccc?key=0At2ExLh4POiZdFpHWXF1UGR1NnlhU21Rd0trTW45dFE&usp=sharing

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
    }
]



See  Google Apps Scripts snippets for more like this

For help and more information join our forumfollow the blogfollow me on twitter

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.




Comments