Find Distance Between Two Points with Apps Script

F
Published in: Google Apps Script - Google Maps

Curious to know how far is location A from location B? You can put the two places in separate cells in a Google Spreadsheet and then use the getDirection() function to find the distance between the two place in miles or kilometers.

Internally, it uses the Google Maps service of Apps Script to find the distance and directions between the two points. You can also use the getMileage() function to calculate the “as the crow flies” distance between any two latitude and longitude co-ordinates.

function getDirection(city1, city2) {

 var directions = Maps.newDirectionFinder()
    .setOrigin(city1).setDestination(city2)
    .setMode(Maps.DirectionFinder.Mode.DRIVING)
    .getDirections();

 var d = directions.routes[0].legs[0].distance.text;

  return parseInt(d.split(" ")[0].replace(",", ""));;
}

function getMileage(city1, city2) {
  var p1 = Maps.newGeocoder().geocode(city1);
  var p2 = Maps.newGeocoder().geocode(city2);

  return getDistance(getCoordinates(p1), getCoordinates(p2), opt);
}

function getCoordinates(point) {
  var result = point.results[0].geometry.location;

  return {"lng": result.lng, "lat": result.lat}
}

function getDistance(c1, c2, opt) {
  var lat1 = rad(c1.lat), lat2 = rad(c2.lat);
  var lng1 = rad(c1.lng), lng2 = rad(c2.lng);
  var dLng = (lng2-lng1), dLat = (lat2-lat1);
  var R = 6371/1.6;

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.sin(dLng/2) * Math.sin(dLng/2) *
    Math.cos(lat1) *  Math.cos(lat2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

  return parseInt(R * c);
}

function rad(degrees) {
  return degrees * Math.PI/180;
}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Google Maps

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube