Skip to content

Instantly share code, notes, and snippets.

@bpwebs
Created May 29, 2023 17:33
Show Gist options
  • Save bpwebs/260fe15094026a9ae4884d4b3a100973 to your computer and use it in GitHub Desktop.
Save bpwebs/260fe15094026a9ae4884d4b3a100973 to your computer and use it in GitHub Desktop.
How to Calculate the Distance Between Two Addresses in Google Sheets
function CALCULATE_DRIVING_DISTANCE(origin, destination) {
var directions = Maps.newDirectionFinder()
.setOrigin(origin)
.setDestination(destination)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
var distance = directions.routes[0].legs[0].distance.value;
distance /= 1000; // Convert meters to kilometers
return Number (distance.toFixed(2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment