This link might be helpful to you, as it details the use of the Haversine formula to calculate the distance.

Excerpt:

This script [in Javascript] calculates great-circle distances between the two points – that is, the shortest distance over the earth’s surface – using the ‘Haversine’ formula.

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}
Answer from user1921 on Stack Overflow
🌐
NOAA
nhc.noaa.gov › gccalc.shtml
Latitude/Longitude Distance Calculator
Enter latitude and longitude of two points, select the desired units: nautical miles (n mi), statute miles (sm), or kilometers (km) and click Compute. Latitudes and longitudes may be entered in any of three different formats, decimal degrees (DD.DD), degrees and decimal minutes (DD:MM.MM) or degrees, minutes, and decimal seconds (DD:MM:SS.SS). Important Note: The distance calculator on this page is provided for informational purposes only.
🌐
Omni Calculator
omnicalculator.com › other › latitude-longitude-distance
Latitude Longitude Distance Calculator
January 18, 2024 - ... d = 2R × sin⁻¹(√[sin²((θ₂ - θ₁)/2) + cosθ₁ × cosθ₂ × sin²((φ₂ - φ₁)/2)]). ... The distance between any two adjacent latitudes is approximately 69 miles or 111 km.
Discussions

algorithm - Calculate distance between two latitude-longitude points? (Haversine formula) - Stack Overflow
How do I calculate the distance between two points specified by latitude and longitude? For clarification, I'd like the distance in kilometers; the points use the WGS84 system and I'd like to unde... More on stackoverflow.com
🌐 stackoverflow.com
How can I calculate the distance between two points using Latitude and Longitude?
u/PercivalFailed - Your post was submitted successfully. Once your problem is solved, reply to the answer(s) saying Solution Verified to close the thread. Follow the submission rules -- particularly 1 and 2. To fix the body, click edit. To fix your title, delete and re-post. Include your Excel version and all other relevant information Failing to follow these steps may result in your post being removed without warning. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/excel
16
6
May 21, 2025
sql - Calculating distance between two points (Latitude, Longitude) - Stack Overflow
I am trying to calculate the distance between two positions on a map. I have stored in my data: Longitude, Latitude, X POS, Y POS. I have been previously using the below snippet. DECLARE @orig_lat More on stackoverflow.com
🌐 stackoverflow.com
How to calculate distance between latitude & longitude coordinates? - Google Docs Editors Community
Skip to main content · Google Docs Editors Help · Sign in · Google Help · Help Center · Community · Google Docs Editors · Terms of Service · Submit feedback · Send feedback on More on support.google.com
🌐 support.google.com
People also ask

Can this tool calculate the distance between two addresses?
Yes. The tool is designed to find the distance between two addresses. Simply enter the start and end addresses in the respective input fields for a complete calculation of both driving and straight-line distances.
🌐
latlongdata.com
latlongdata.com › home › distance calculator
Distance Calculator - Latitude Longitude Data
What is the distance between two consecutive latitudes?

The distance between any two adjacent latitudes is approximately 69 miles or 111 km. Latitude lines run parallel to each other. That's why the distance between them remains constant from the South to the North pole. On the other hand, longitude lines are furthest apart at the equator and meet at the poles.

🌐
omnicalculator.com
omnicalculator.com › other › latitude-longitude-distance
Latitude Longitude Distance Calculator
What is the difference between straight-line distance and driving distance?
Straight-line distance is the shortest geographical path between two points, ignoring all obstacles. Driving distance is the actual distance traveled along a road network, which is almost always longer. The tool provides both measurements for comprehensive planning.
🌐
latlongdata.com
latlongdata.com › home › distance calculator
Distance Calculator - Latitude Longitude Data
🌐
Movable Type
movable-type.co.uk › scripts › latlong.html
Calculate distance and bearing between two Latitude/ ...
Just as the initial bearing may vary from the final bearing, the midpoint may not be located half-way between latitudes/longitudes; the midpoint between 35°N,45°E and 35°N,135°E is around 45°N,90°E. An intermediate point at any fraction along the great circle path between two points can also be calculated.1 · Given a start point, initial bearing, and distance, this will calculate the destina­tion point and final bearing travelling along a (shortest distance) great circle arc.
🌐
Microsoft Fabric Community
community.fabric.microsoft.com › t5 › Desktop › How-to-calculate-lat-long-distance › td-p › 1488227
Solved: How to calculate lat/long distance - Microsoft Fabric Community
June 29, 2024 - This calculates distance (in kilometers) between two points on the surface of the Earth as a sphere. ... There may also be some inaccuracy from rounding (I get slightly different results when using a calculator) ... Use Google Earth. ... Not terribly helpful... . One set of points? Sure. But if you have 50 and you want the top 5 shortest? User won't wait while you run that api 50 times to determine the shortest. ... You need Latitude and Longitude to calculate the distance between two locations with following formula:
🌐
Latitude Longitude Data
latlongdata.com › home › distance calculator
Distance Calculator - Latitude Longitude Data
November 6, 2025 - The calculator is flexible and accepts both full addresses and latitude/longitude coordinates. To use coordinates, enter them in decimal format (e.g., 40.7128, -74.0060) to calculate distance between two coordinates directly. The tool can measure distances globally. However, for extremely long driving routes that span continents or require complex ferry connections, the driving distance estimation may be less precise or unavailable. The straight-line distance calculation remains accurate for any two points on Earth.
Find elsewhere
Top answer
1 of 16
1405

This link might be helpful to you, as it details the use of the Haversine formula to calculate the distance.

Excerpt:

This script [in Javascript] calculates great-circle distances between the two points – that is, the shortest distance over the earth’s surface – using the ‘Haversine’ formula.

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}
2 of 16
519

I needed to calculate a lot of distances between the points for my project, so I went ahead and tried to optimize the code, I have found here. On average in different browsers my new implementation runs 2 times faster than the most upvoted answer.

function distance(lat1, lon1, lat2, lon2) {
  const r = 6371; // km
  const p = Math.PI / 180;

  const a = 0.5 - Math.cos((lat2 - lat1) * p) / 2
                + Math.cos(lat1 * p) * Math.cos(lat2 * p) *
                  (1 - Math.cos((lon2 - lon1) * p)) / 2;

  return 2 * r * Math.asin(Math.sqrt(a));
}

You can play with my jsPerf and see the results here.

Recently I needed to do the same in python, so here is a python implementation:

from math import cos, asin, sqrt, pi

def distance(lat1, lon1, lat2, lon2):
    r = 6371 # km
    p = pi / 180

    a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2
    return 2 * r * asin(sqrt(a))

And for the sake of completeness: Haversine on Wikipedia.

🌐
Reddit
reddit.com › r/excel › how can i calculate the distance between two points using latitude and longitude?
r/excel on Reddit: How can I calculate the distance between two points using Latitude and Longitude?
May 21, 2025 -

I have a list of locations and their latitude and longitudes. I want to compare them to a specific point and use a formula to output their distances from that point in terms of how many miles East and North of that point they are, not in a straight line beween them.

So I'd have my origin point and point A. Lines 2 and 3 would look something like this:

Name of Origin point | latitude O | longitude O | 0 | 0

Name of Point A | latitude A | longitude A | X (number of miles) | Y (number of miles)

What formulas would I use in columns D and E to calculate these distances?

Top answer
1 of 11
9
This may be slightly more complicated than you're expecting, and I apologize for not directly providing any formulas as I'm on mobile. The easy one is your northing. Latitude lines are all parallal, and what you could do is convert your start and end points from coordinate to radians, relative to the equator. Then multiply that difference by the radius of the earth. To be hyper-technical, the earth is not a sphere, but an oblate spheroid. Without the context of the distances you're covering, I would be shocked if that ended up making a practical difference. Easting is slightly more complicated, since the distance between longitude lines gets smaller the closer to the poles you are (I will assume you're referring to distances in the northern hemisphere). Again, the effect of this will depend on the distances being considered. The calculation of this distance would again be coordinate to radian, multiply by radius, however, this circle would only be the radius of the earth if the coordinates were on the equator. You would need a calculation to determine the radius of the horizontal section of the earth that that northing coordinate makes. You will get different answers depending on if you calculate this distance at the start or end northing. You'll have to use your judgement on which is appropriate for your use case. For the actual calculations, assuming you want these constrained to their own column and not have to create multiple dummy columns, the LET() function is your friend here.
2 of 11
6
Like this? Haversine formula seems to work... || || |Place|Latitude|Longitude|Distance KM|Distance Mi| |Toronto|43.651070|-79.347015|510.6768877|317.3234442| |Montreal-EST|45.630001|-73.519997||| Here you go: https://pastebin.com/raw/Cs72CSAJ
Top answer
1 of 6
149

Since you're using SQL Server 2008, you have the geography data type available, which is designed for exactly this kind of data:

CopyDECLARE @source geography = 'POINT(0 51.5)'
DECLARE @target geography = 'POINT(-3 56)'

SELECT @source.STDistance(@target)

Gives

Copy----------------------
538404.100197555

(1 row(s) affected)

Telling us it is about 538 km from (near) London to (near) Edinburgh.

Naturally there will be an amount of learning to do first, but once you know it it's far far easier than implementing your own Haversine calculation; plus you get a LOT of functionality.


If you want to retain your existing data structure, you can still use STDistance, by constructing suitable geography instances using the Point method:

CopyDECLARE @orig_lat DECIMAL(12, 9)
DECLARE @orig_lng DECIMAL(12, 9)
SET @orig_lat=53.381538 set @orig_lng=-1.463526

DECLARE @orig geography = geography::Point(@orig_lat, @orig_lng, 4326);

SELECT *,
    @orig.STDistance(geography::Point(dest.Latitude, dest.Longitude, 4326)) 
       AS distance
--INTO #includeDistances
FROM #orig dest
2 of 6
54

The below function gives distance between two geocoordinates in miles

Copycreate function [dbo].[fnCalcDistanceMiles] (@Lat1 decimal(8,4), @Long1 decimal(8,4), @Lat2 decimal(8,4), @Long2 decimal(8,4))
returns decimal (8,4) as
begin
declare @d decimal(28,10)
-- Convert to radians
set @Lat1 = @Lat1 / 57.2958
set @Long1 = @Long1 / 57.2958
set @Lat2 = @Lat2 / 57.2958
set @Long2 = @Long2 / 57.2958
-- Calc distance
set @d = (Sin(@Lat1) * Sin(@Lat2)) + (Cos(@Lat1) * Cos(@Lat2) * Cos(@Long2 - @Long1))
-- Convert to miles
if @d <> 0
begin
set @d = 3958.75 * Atan(Sqrt(1 - power(@d, 2)) / @d);
end
return @d
end 

The below function gives distance between two geocoordinates in kilometres

CopyCREATE FUNCTION dbo.fnCalcDistanceKM(@lat1 FLOAT, @lat2 FLOAT, @lon1 FLOAT, @lon2 FLOAT)
RETURNS FLOAT 
AS
BEGIN

    RETURN ACOS(SIN(PI()*@lat1/180.0)*SIN(PI()*@lat2/180.0)+COS(PI()*@lat1/180.0)*COS(PI()*@lat2/180.0)*COS(PI()*@lon2/180.0-PI()*@lon1/180.0))*6371
END

The below function gives distance between two geocoordinates in kilometres using Geography data type which was introduced in sql server 2008

CopyDECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SET @h = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @g.STDistance(@h);

Usage:

Copyselect [dbo].fnCalcDistanceKM

Reference: Ref1,Ref2

🌐
Google Support
support.google.com › docs › thread › 137329980 › how-to-calculate-distance-between-latitude-longitude-coordinates
How to calculate distance between latitude & longitude coordinates? - Google Docs Editors Community
Skip to main content · Google Docs Editors Help · Sign in · Google Help · Help Center · Community · Google Docs Editors · Terms of Service · Submit feedback · Send feedback on
🌐
AskPython
askpython.com › home › distance between two geo-locations in python
Distance Between Two Geo-Locations in Python - AskPython
March 30, 2023 - To calculate the distance between two points based on latitude and longitude in Python, you can use various methods such as the Haversine formula, math module, geodesic distance, or the great circle distance.
🌐
MathWorks
mathworks.com › mapping toolbox › geometric geodesy › great circles, geodesics, and rhumb lines
distance - Distance between points on sphere or ellipsoid - MATLAB
When you specify a reference ellipsoid as input to the distance function, the function returns linear distances in the units of the semimajor axis of the ellipsoid. ... Latitude of the start point, specified as a scalar or array. You can specify lat1, lon1, lat2, and lon2 using a combination of scalars and arrays, as long as the arrays are of the same size. The function expands the scalar inputs to match the size of the array inputs. ... Longitude of the start point, specified as a scalar or array.
🌐
Niotron Community
community.niotron.com › discuss
Calculate the distance between two locations using latitude and longitude lines within the application help - Discuss - Niotron Community
June 21, 2024 - Calculate the distance between two locations using latitude and longitude lines within the application help I used many rules, but the answer is different. I’ve been searching for days. I think this link is useful …
🌐
AuraQ
auraq.com › distance-calculations-using-latitudes-and-longitudes
Distance calculations using latitudes and longitudes - AuraQ
October 18, 2022 - For this we need to use the Haversine formula. The Haversine formula finds great circle distance between two points given their latitudes and longitudes.
🌐
USGS
usgs.gov › faqs › how-much-distance-does-a-degree-minute-and-second-cover-your-maps
How much distance does a degree, minute, and second cover on your maps? | U.S. Geological Survey
At 38 degrees North latitude (which passes through Stockton California and Charlottesville Virginia): One degree of latitude equals approximately 364,000 feet (69 miles), one minute equals 6,068 feet (1.15 miles), and one-second equals 101 feet.
🌐
Medium
medium.com › @manishsingh7163 › calculating-distance-between-successive-latitude-longitude-coordinates-using-pandas-287c15bc5029
Calculating Distance Between Successive Latitude-Longitude Coordinates using Pandas | by Manish Singh | Medium
March 18, 2024 - In this blog post, we’ll explore how to achieve this using Python, specifically with Pandas and the Haversine formula. ... The Haversine formula is a mathematical formula used to calculate the shortest distance between two points on a sphere ...
🌐
Sisense
sisense.com › home › blog › latitude and longitude distance calculation explained
Latitude-Longitude Distance Calculation: Methods Explained
March 18, 2026 - One of the most common ways to calculate distances using latitude and longitude is the haversine formula, which is used to measure distances on a sphere. This method uses spherical triangles and measures the sides and angles of each to calculate ...
🌐
GPS Visualizer
gpsvisualizer.com
GPS Visualizer
GPS Visualizer is a free utility that creates customizable maps and profiles from GPS data (tracklogs & waypoints), addresses, or coordinates.
🌐
GIS Geography
gisgeography.com › home › maps & cartography › latitude, longitude and coordinate system grids
Latitude, Longitude and Coordinate System Grids - GIS Geography
March 10, 2024 - Latitude is the distance north/south away from the equator. ... Right you say! that’s mean value of Latitude is along the Longitude or Meridian line and value of Longitude is along the Latitude or Parallel because of Lat and Long are perpendicular each other ok! For those referring to (land?) Plot plans, these use a different datum(?) than geographic coordinates. They are based on bearings, not locations in (x,y,z), and depend on a fixed starting point other than (0,0,0).
🌐
ThoughtCo
thoughtco.com › degree-of-latitude-and-longitude-distance-4070616
The Distance Between Two Latitude and Longitude Coordinates
May 18, 2024 - You could use what is known as a haversine formula to calculate the distance—but unless you are a whiz at trigonometry, it is not easy. Luckily, in today's digital world, computers can do the math for us. Most interactive map applications will allow you to input GPS coordinates of latitude and longitude and tell you the distance between the two points...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › program-distance-two-points-earth
Program for distance between two points on earth - GeeksforGeeks
February 13, 2023 - For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, ...