Calculate distance between 2 sets of geographic coordinates

1

Please login or register to vote for this query.

(click on this box to dismiss)

Calculates the distance between two sets of geographic (map) coordinates (Lat/Lon in decimal format).

Graphic Design

Q&A for Graphic Design professionals, students, and enthusiasts

--calculate distance/bearing/midpoint/more: https://www.movable-type.co.uk/scripts/latlong.html

declare @lat1 float = ##lat1:float?51.5077##
declare @lon1 float = ##lon1:float?-0.1274## 
declare @lat2 float = ##lat2:float?42.9855##
declare @lon2 float = ##lon2:float?-81.2453##

declare @R float = 6371e3; --metres
declare @φ1 float = radians(@lat1);
declare @φ2 float = radians(@lat2);
declare @Δφ float = radians(@lat2-@lat1);
declare @Δλ float = radians(@lon2-@lon1);

declare @a float = sin(@Δφ/2) * sin(@Δφ/2) +
                   cos(@φ1) * cos(@φ2) *
                   sin(@Δλ/2) * sin(@Δλ/2);
declare @c float = 2 * atn2(sqrt(@a), sqrt(1-@a));

declare @d float = @R * @c;

print @d/1000 --km

--lat1:Location 1 (Latitude) : "Enter the latitude (NS/Y) coordinate for location #1 in decimal format (-90.0000 to +90.0000)"
--lon1:Location 1 (Longitude):"Enter the longitude (EW/X) coordinate for location #1 in decimal format (-180.0000 to +180.0000)"
--lat2:Location 2 (Latitude) : "Enter the latitude (NS/Y) coordinate for location #2 in decimal format (-90.0000 to +90.0000)"
--lon2:Location 2 (Longitude):"Enter the longitude (EW/X) coordinate for location #2 in decimal format (-180.0000 to +180.0000)"

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results