Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

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. Is there a function in Google Spreadsheet that can do this?

I am looking for "distance driven", not "as the crow flies".

share|improve this question

migrated from superuser.com Jul 1 '11 at 16:57

This question came from our site for computer enthusiasts and power users.

    
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. –  GalacticCowboy May 12 '10 at 0:11
    
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. –  landon9720 Jul 11 '12 at 16:39

6 Answers 6

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.

share|improve this answer
    
How do you get the API key? –  Jacob Jan Tuinstra Jul 1 '13 at 17:46
    
@JacobJanTuinstra developer.mapquest.com –  Neil McGuigan Jul 1 '13 at 19:18
    
Neil, your alive !! Thanks for sharing that. –  Jacob Jan Tuinstra Jul 1 '13 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."; –  nbolton Nov 4 at 13:49

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

share|improve this answer

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.

share|improve this answer
    
yes, but what about distance driven instead of "as the crow flies"? –  Rosarch Oct 18 '09 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 '09 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 '09 at 18:34
    
OP uses Google Spreadsheets. –  Jacob Jan Tuinstra Jul 1 '13 at 17:53
    
+1 because of the interesting formula, however I think using this is probably impractical. –  nbolton Nov 4 at 13:42

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.

share|improve this answer

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

share|improve this answer

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/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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