Using the GPS commands to track distance

Post Reply
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:

Using the GPS commands to track distance

Post by rbytes »

I'm hoping that someone in sB Land will be able to help me with this project. I do a lot of walking, and for several years I had an app that would announce my progress every quarter kilometer. I found it very encouraging to hear this information. But alas, the app, like many others on the App Store, has switched from a single purchase to a monthly or yearly membership, and it is not cheap!

But smart BASIC should be up to the task. The problem is to find a formula that works to calculate distance from two latitudes and longitudes, and to correctly enter it into sB commands. I did an hour of searching last night, and this was the closest I came:
If you want to find the distance between two points just use this formula and you will get the result in Km, just convert to miles if needed.

Point A: LAT1, LONG1 Point B: LAT2, LONG2

ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-lat2)) *COS(RADIANS(long1-long2)))*6371
But I don't see how to code it in sB, since Radians is an option setting, not a function. I'd appreciate any insights anyone can give.
The only thing that gets me down is gravity...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Using the GPS commands to track distance

Post by Henko »

Code: Select all

option angle degrees
read a_lat,a_lon,b_lat,b_long
data 52.383, 4.915,  40.97, -73.94  ' Amsterdam, New York
print geo_distance(a_lat,a_lon,b_lat,b_long)
end

def geo_distance (a_lat,a_lon,b_lat,b_lon)
tt=sin(a_lat)*sin(b_lat)+cos(a_lat)*cos(b_lat)*cos(abs(b_lon-a_lon))
return 111.12*acos(tt)   ' km
end def

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Using the GPS commands to track distance

Post by Dutchman »

ricardobytes wrote:……
If you want to find the distance between two points just use this formula and you will get the result in Km, just convert to miles if needed.
Point A: LAT1, LONG1 Point B: LAT2, LONG2
ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-lat2)) *COS(RADIANS(long1-long2)))*6371
But I don't see how to code it in sB, since Radians is an option setting, not a function. I'd appreciate any insights anyone can give.
In the formula ACOS(COS(RADIANS(90-Lat1))… the function RADIANS(x) means that x should be interpreted in radians instead of degrees.
So if you set in your program the option 'OPTION ANGLE RADIANS' then you can apply the formula as ACOS(COS(90-Lat1)…

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Using the GPS commands to track distance

Post by Henko »

Dutchman wrote:
ricardobytes wrote:……
If you want to find the distance between two points just use this formula and you will get the result in Km, just convert to miles if needed.
Point A: LAT1, LONG1 Point B: LAT2, LONG2
ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-lat2)) *COS(RADIANS(long1-long2)))*6371
But I don't see how to code it in sB, since Radians is an option setting, not a function. I'd appreciate any insights anyone can give.
In the formula ACOS(COS(RADIANS(90-Lat1))… the function RADIANS(x) means that x should be interpreted in radians instead of degrees.
So if you set in your program the option 'OPTION ANGLE RADIANS' then you can apply the formula as ACOS(COS(90-Lat1)…
Looking at the "90" values in the formula, do you really think that the formula should be used in the radians mode? Try the degrees mode, as i did in my code snippet and check the results. :idea:

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Using the GPS commands to track distance

Post by Henko »

On second thought, the author of this formula assumed that the Basic interpreter takes radians only in goniometric functions, hence the conversion from degrees to radians using a function RADIANS(x) (which does not exist in SB, because it is not needed).

I don't understand why the author doesn't simplify the formula a bit, using the identities
COS(90-x) = SIN(x) etc.

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Using the GPS commands to track distance

Post by Henko »

On second thought, the author of this formula assumed that the Basic interpreter takes radians only in goniometric functions, hence the conversion from degrees to radians using a function RADIANS(x) (which does not exist in SB, because it is not needed).

I don't understand why the author doesn't simplify the formula a bit, using the identities
COS(90-x) = SIN(x) etc. In fact the formula is exactly the same as i used in the code snippet, be it that he forgot the absolute function and he uses another constant. May be the result is given in yards, or furlongs or whatever.

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: Using the GPS commands to track distance

Post by rbytes »

Thanks for the quick responses. Who needs Wolfram Alpha when this site has such knowledgeable members! :D

Henko's formula gives a result very close to the Amsterdam-New York distance that I found on several online maps. They were all slightly higher, so it may be that the formula doesn't fully factor in earth's curvature. But for the distances of my walks, the error is insignificant.

I will proceed to add the talking feature and will post my program when it is complete.
The only thing that gets me down is gravity...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Using the GPS commands to track distance

Post by Henko »

ricardobytes wrote: Henko's formula gives a result very close to the Amsterdam-New York distance that I found on several online maps. They were all slightly higher, so it may be that the formula doesn't fully factor in earth's curvature. But for the distances of my walks, the error is insignificant.
The difference can be explained by the fact that i took 2 arbitairy points in the amsterdam area and in the New York area using google maps. Hence a difference until 20 kilometers would be quite possible.

Post Reply