Calculating distance

abastien

Member
Licensed User
Here is some sample code to calculate distance between two points:

Code: Copy
Sub getDistanceKM(lat1, long1, lat2, long2)

   DEGREES_TO_RADIANS = (cPI / 180.0)
   EARTH_RADIUS = 6371.0
   
   rlat1 = DEGREES_TO_RADIANS * lat1
   rlong1 = DEGREES_TO_RADIANS * long1
   rlat2 = DEGREES_TO_RADIANS * lat2
   rlong2 = DEGREES_TO_RADIANS * long2
   
   '   There is no real reason To break this lot into 
   '   4 statements but I just feel it's a little more 
   '   readable.
   p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
   p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
   p3 = Sin(rlat1) * Sin(rlat2)

   ret = p1 + p2 + p3
   If ret = 1 Then Return 0
   
   ret = ACos(ret)
   ret = ret * EARTH_RADIUS
   
   Return ret
   
End Sub

Can we add this to the gps.dll?
 
Generally speaking, libraries should only include functions that it isn't possible to implement from code.

I was thinking of the processing power. Would complex calculations not run faster in a library?

Just a thought - not based on fact.
 
Hi there,

Just a little correction for your code

" If ret >1 Then Return 0 "

My tests give me sometime 1.0000000002 :-)

Bye,

Bruno.
 
Thanks for the info klaus,
I was already searching for the mathematical/physical/geographical basis of this routine, since for me, using existing routines is great, but i like to understand what im using :)

edit://
Generally speaking, libraries should only include functions that it isn't possible to implement from code.

arent all functions of the gps lib (at least in the version i have which i guess is the latest/only one) replaceable by string operations that can be done with the b4p standart string functions set?
 
Last edited:
arent all functions of the gps lib (at least in the version i have which i guess is the latest/only one) replaceable by string operations that can be done with the b4p standart string functions set?
Yes.
Every statement has one or two exceptions ;)

If I were to write the GPS library today I would have written it as a module instead of a library. That would have made it easier to enhance it and parse other NMEA sentences.
The GPS library was written before Basic4ppc V6.50 and therefore modules weren't available at that time.
 
Back
Top