This guide walks you through how to calculate the distance between two points in a spreadsheet using the haversine distance formula, which is also called straight-line or as-the-crow-flies distance. (It is not driving distance.)
We're using Excel and Google Sheets, but a similar approach should also work in Numbers.
This guide assumes you're using Geocodio for your calculations and that latitude/longitude are in separate columns. Since Geocodio provides decimal degrees, all examples use decimal degrees. We've provided workbooks for Excel and Google Sheets with worked examples as a companion to this guide. To get the workbooks, enter your email above.
This guide walks you through how to:
This is the general process you'll need to follow for calculating distance in spreadsheets.
Upload your spreadsheet here. Geocodio will return decimal coordinates using WGS-84.
Let's say you have a distribution center and you want to know approximately how far away each of your retail stores are.
Depending on the number of locations you have, you can put them in one spreadsheet. If you have a lot of locations, you might have multiple sheets within the same workbook. Our example provides templates for both.
You will need to choose whether you want miles or kilometers. We have provided examples for both.
These instructions use Geocodio's template workbooks for Excel and Google Sheets with worked examples of distance calculations, including formulas. To get the spreadsheets, enter your email in the form above and we'll send it to you.
This is the most basic distance calculation and is useful for finding the distance between two specific locations using coordinates from the same row. For example, you might need to find the shipping distance between two specific addresses, how far a customer lives from a particular store, or how far an employee lives from their work location.
Output the distance in miles:
=ACOS(COS(RADIANS(90-A2))*COS(RADIANS(90-D2))+SIN(RADIANS(90-A2))*SIN(RADIANS(90-D2))*COS(RADIANS(B2-E2)))*3959
Output the distance in kilometers:
=ACOS(COS(RADIANS(90-A2))*COS(RADIANS(90-D2))+SIN(RADIANS(90-A2))*SIN(RADIANS(90-D2))*COS(RADIANS(B2-E2)))*6371
This method finds the closest location from a single reference point to multiple destinations, using the structure from "Many-to-One or One-to-many" sheet. You can also use it to find the relationship between many points and one specific point. For example, you might have a GPS device in a vehicle that has broken down, and want to know which of your service centers are closest to the breakdown site.
Using miles:
=ACOS(COS(RADIANS(90-$A$2))*COS(RADIANS(90-'List B'!E2))+SIN(RADIANS(90-$A$2))*SIN(RADIANS(90-'List B'!E2))*COS(RADIANS($B$2-'List B'!F2)))*3959
Kilometers:
=ACOS(COS(RADIANS(90-$A$2))*COS(RADIANS(90-'List B'!E2))+SIN(RADIANS(90-$A$2))*SIN(RADIANS(90-'List B'!E2))*COS(RADIANS($B$2-'List B'!F2)))*6371
Where:
$A$2, $B$2
= Reference point coordinates (locked with $ signs)'List B'!E2, F2
= Each destination's coordinatesThis uses the street address as the name. You can use anything else for the name, such as a store number. (When you upload a file to Geocodio, we only add columns, so you can leave in any necessary references and they'll still be there when you download your file.)
=INDEX('List B'!A:A,MATCH(MIN(G:G),G:G,0))
This is Cell K2 in the example.
=MIN(G:G)
This is Cell K3 in the example.
=MAX(G:G)
Given two lists, this method uses List A as the input and finds the closest location on List B. This example takes a location from "List A Geocodio Results" (Duluth GA, Los Angeles CA, etc.) and finds the closest location in "List B Geocodio Results" (Fort Worth TX, Gillette WY, etc.). This can be used for customer-to-store assignment, territory planning, and supply chain optimization between facilities.
Please note that this is a complex formula with significant processing overhead. For lists with more than 100 input/matching locations (ie., 100 inputs, 100 possible matches), consider using a database, as Excel or Google Sheets will likely freeze.
Column A (List A Location Reference) - Cell A2: First, pull in the reference for the location you're starting from:
='List A'!A2
Column B (Closest List B Match) - Cell B2: Then scan all of List B to find the closest location:
=INDEX('List B'!A:A,MATCH(SMALL(IF(ROW('List B'!A:A)>=2,ACOS(COS(RADIANS(90-'List A'!E2))*COS(RADIANS(90-'List B'!E:E))+SIN(RADIANS(90-'List A'!E2))*SIN(RADIANS(90-'List B'!E:E))*COS(RADIANS('List A'!F2-'List B'!F:F)))*3959),2),IF(ROW('List B'!A:A)>=2,ACOS(COS(RADIANS(90-'List A'!E2))*COS(RADIANS(90-'List B'!E:E))+SIN(RADIANS(90-'List A'!E2))*SIN(RADIANS(90-'List B'!E:E))*COS(RADIANS('List A'!F2-'List B'!F:F)))*3959),0))
Column C (Distance in Miles) - Cell C2: Output the distance in miles:
=ACOS(COS(RADIANS(90-'List A'!E2))*COS(RADIANS(90-INDEX('List B'!E:E,MATCH(B2,'List B'!A:A,0))))+SIN(RADIANS(90-'List A'!E2))*SIN(RADIANS(90-INDEX('List B'!E:E,MATCH(B2,'List B'!A:A,0))))*COS(RADIANS('List A'!F2-INDEX('List B'!F:F,MATCH(B2,'List B'!A:A,0)))))*3959
Column D (Distance in Kilometers) - Cell D2: Output the distance in kilometers:
=C2*1.60934
This alternative does not use array formulas, which are not supported by all versions of Excel. Because of that, Google Sheets is the recommended method.
In "List B Geocodio Results", add helper column G with this formula in G2:
=IF(AND(E2='List A'!$E$2,F2='List A'!$F$2),999999,ACOS(COS(RADIANS(90-'List A'!$E$2))*COS(RADIANS(90-E2))+SIN(RADIANS(90-'List A'!$E$2))*SIN(RADIANS(90-E2))*COS(RADIANS('List A'!$F$2-F2)))*3959)
Then use INDEX/MATCH to find minimum distance:
=INDEX('List B'!A:A,MATCH(MIN('List B'!G:G),'List B'!G:G,0))
$E$1
) for fixed reference points in one-to-many calculationsSources
We are indebted to the formulas written here.
You can read more about the benefits and limitations of the haversine formula and a discussion of other formulas here.