9

I'm working with Google Docs and want to create a spreadsheet of the following form:

Point A         Point B           Mileage
Los Angeles     Miami             292100
Palo Alto       San Francisco     90

In the spreadsheet, Mileage is automatically calculated. I am looking for "distance driven", not "as the crow flies".

Is there a function in Google Spreadsheets that can do this?

4
  • As the distance increases, the difference between "driving distance" and "crow flight" will diminish accordingly. In any case they probably won't differ by more than a few percent.
    – user1361
    May 12, 2010 at 0:11
  • 1
    that seems like a pretty bold claim, cowboy. are you saying that driving from portland to atlanta is is roughly the same milage as flying? i don't think so. Jul 11, 2012 at 16:39
  • 1
    @pnuts agreed, and i'm not gonna defend my 4 year old comment :) Jan 9, 2015 at 21:11
  • Rosarch, why don't you revisit Web Applications after 4 years of absence and review all the nice answers you received on your question? Apr 6, 2015 at 12:43

8 Answers 8

9

With the following little piece of code, you can retrieve the distance between two points.

Code

function getDirection(city1, city2, unit) {
  var directions = Maps.newDirectionFinder()
    .setOrigin(city1).setDestination(city2)
    .setMode(Maps.DirectionFinder.Mode.DRIVING)
    .getDirections();
  var d = directions.routes[0].legs[0].distance.text;

  var distance, value = d.split(" ")[0].replace(",", ""), text = d.split(" ")[1];
  if(text == unit) {
    distance = value;
  } else if(text == "km" && unit == "mi") {
    distance = value / 1.6;
  } else {
    distance = value * 1.6;
  }
  return Math.round(distance);
}

Explained

The MAPs API, build on top of the Google Apps Script, will get the first direction (routes[0]) between the origin and destination. The returned value is stripped from comma's and parsed as an integer.

Screenshot

enter image description here

Note

The distance returned can be either in kilometer or miles. I've added the calculation for "as the crow flies" as well (in miles).

Add the script under Tools>Script editor in the spreadsheet. Press the bug button in the script editor and you're on the go !!

Example

I've created an example file for you: get driving distance

References

1
  • 1
    It looks like the formula in the sample doc is no longer working consistently. Could someone please edit it to make it work? I know nothing about scripting (more than copying and pasting) and I would love your help! All I'm looking for is the driving distance between zip codes. Thank you!
    – Melissa
    Nov 3, 2016 at 22:19
3

In Google Spreadsheets:

=importXML("http://mapquestapi.com/directions/v1/route?  
  key=YOUR_KEY_HERE&outFormat=xml&from=" & A2 & "&to=" 
  & B2,"//response/route/distance")

where A2 is the origin and B2 is the destination.

You have to get an API key from mapquest and replace the value YOUR_KEY_HERE with it.

4
  • 1
    How do you get the API key? Jul 1, 2013 at 17:46
  • 1
    @JacobJanTuinstra developer.mapquest.com Jul 1, 2013 at 19:18
  • Neil, your alive !! Thanks for sharing that. Jul 1, 2013 at 19:21
  • +1 Apparently elegant solution, however MapQuest is a little lousy. I got this error when trying to use the key I was given: "This key is not authorized for this service. If you do not have a key, you can obtain a free key by registering at developer.mapquest.com." Nov 4, 2014 at 13:49
1

This is what works for me:

=importXML("http://maps.googleapis.com/maps/api/distancematrix/xml?&origins="& <point.A> & "&destinations=" & <point.B>; "//distance/value")

This will yield the driven distance between <point.A> and <point.B> in meters (according to Google Maps, of course).

2
  • This is great! How about the as-the-crow-flies distance? Nov 29, 2017 at 14:36
  • This needs a API key too. Least now.. Oct 24, 2022 at 20:19
0

I do not think there is built in Excel function, or extension, to do this.

You might want to check out this Source Forge page: http://zips.sourceforge.net/

  • It provides a CSV dataset to convert Zip Codes to Lat/Long (center point presumably)
  • It provides Excel code to calculate the "crow flies" distance between two lat/long coordinates. (and includes equivalent code for Java, PHP and Python.

The Excel function is

=IF(SIN(Lat1) * SIN(Lat2) + COS(Lat1) * COS(Lat2) * COS(Long1 - Long2) > 1,
  RadiusofEarth * ACOS(1), RadiusofEarth *
  ACOS(SIN(Lat1) * SIN(Lat2) + COS(Lat1) * COS(Lat2) * COS(Long1-Long2)))

The page notes that the angles have to be in Radians and that Excel has a degrees to Radians conversion function:

=RADIAN(degrees)

Unless someone else has a slicker answer, this should get you what you want without too much work.

5
  • yes, but what about distance driven instead of "as the crow flies"?
    – Rosarch
    Oct 18, 2009 at 16:08
  • 1
    That is considerable more difficult and would require knowledge of all roadways, as is done in Mapquest, Google Earth, Streets&Trips(or MapPoint). I don't know if any of these apps have a suitable API.
    – Kije
    Oct 18, 2009 at 18:30
  • ... on the other hand, if you were looking for a rough and ready approximation for a fairly limited number of cities, you could look for the driving distance tables that are fairly common in driving maps and atlases. Replicating that style of data in Excel and constructing a lookup to retrieve it for pairs of locations is certainly feasible in Excel. If that appeals, reply and I will could work up a function for it.
    – Kije
    Oct 18, 2009 at 18:34
  • 1
    OP uses Google Spreadsheets. Jul 1, 2013 at 17:53
  • +1 because of the interesting formula, however I think using this is probably impractical. Nov 4, 2014 at 13:42
-1

For a complete Google spreadsheet example, in the form of a spreadsheet template, plus information about the Google Maps API: http://winfred.vankuijk.net/2010/12/calculate-distance-in-google-spreadsheet/

-1

Google Developers offers a very robust script for map related functions in Google Sheets that I used and have really enjoyed. Full instructions are on the webpage as well.

1
  • 1
    Could you summarize some of the key points of the "full instructions," please? Answers should stand alone and not depend on external sources (though I realize it's less likely that a Google site will disappear). Thanks!
    – jonsca
    Feb 13, 2018 at 23:01
-2

I am not familiar enough with the APIs to tell you exactly how to do this, but I would think it would be possible using the Google Maps API.

Google Maps, of course, can provide directions from one point to another through their API. It would be complicated, but possible to route the locations from the spreadsheet through the API and get driving directions, which would give you a driving distance between cities.

-2

You can write the functions on your own which is similar like macro's

http://www.google.com/google-d-s/scripts/yourfirstscript.html

you can find the full document here

PS: Currently It doesn't open for all Google Apps Script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.