Calculating Distances using Latitude and Longitude (Haversine Formula)

Post Reply
User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Calculating Distances using Latitude and Longitude (Haversine Formula)

Post by GeorgeMcGinn »

Here is a function that calculates the haversine, or distances between two points using the Latitude and Longitude of each location.

Useful when programming GPS coordinates and you need to know how far you are from your destination, or to calculate the distance between two cities, as in the code wrapping this function.

The function calculates Kilometers, but has the option (passed as UNIT$) to calculate in Miles or Nautical Miles.

George.

Code: Select all

'*** Haversine Function
'*** George McGinn, January 2017


'*** LAT/LONG For Venice, FL
Lat1=27.07
Lon1=-82.44

'*** LAT/LONG For Sarasota, FL
Lat2=27.40   
Lon2=-82.55

'*** Units: K=kilometers  M=miles  N=nautical miles
Unit$ = "M"	

Result=Haversine(Lat1,Lon1,Lat2,Lon2,Unit$)

PRINT "The Distance From Venice, FL to Sarasota, FL in Miles is: "&Result

DEF Haversine(Lat1,Lon1,Lat2,Lon2,Unit$)
    PI=3.14159265358979323846
    Radius=6378.137
    Lat1=(Lat1*PI/180)
    Lon1=(Lon1*PI/180)
    Lat2=(Lat2*PI/180)
    Lon2=(Lon2*PI/180)
    DLon=Lon1-Lon2
    Answer=ACOS(SIN(Lat1)*SIN(Lat2)+COS(Lat1)*COS(Lat2)*COS(DLon))*Radius

    IF UNIT$="M" THEN Answer=Answer*0.621371192
    IF UNIT$="N" THEN Answer=Answer*0.539956803

  RETURN Answer
ENDDEF
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Calculating Distances using Latitude and Longitude (Haversine Formula)

Post by rbytes »

Very useful. Thanks, George.
The only thing that gets me down is gravity...

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Calculating Distances using Latitude and Longitude (Haversine Formula)

Post by GeorgeMcGinn »

Sorry, one thing I did not point out is that the answer without the modifications of the if statements is always in kilometers.

So in my print statement, you need to change "miles" to the unit you select. (Even though it only checks for M or N, you can put a K for kilometers or any other value.

George.
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

Post Reply