  # Calculating distance

Discussion in 'Code Samples & Tips' started by abastien, May 6, 2008.

1. Here is some sample code to calculate distance between two points:

Code:
`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?

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

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

Just a thought - not based on fact.

4. Calculations will run faster in a library but the difference should not be too significant.

5. Hi there,

Just a little correction for your code

" If ret >1 Then Return 0 "

My tests give me sometime 1.0000000002 Bye,

Bruno.

Am I right assuming that a decimal lat/lon input is required?
I think the other (dddmm.mm) format is giving me false distances...

Last edited: Jan 8, 2009

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://
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: Jan 9, 2009
8. 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.