# Calculating distance

#### abastien

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

B4X:
Sub getDistanceKM(lat1, long1, lat2, long2)

'   There is no real reason To break this lot into
'   4 statements but I just feel it's a little more
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)

Return ret

End Sub
Can we add this to the gps.dll?

#### Erel

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

#### abastien

##### Member
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.

#### Erel

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

#### Bruno

##### Member
Hi there,

Just a little correction for your code

" If ret >1 Then Return 0 "

My tests give me sometime 1.0000000002

Bye,

Bruno.

#### MM2forever

##### Active Member
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:

#### MM2forever

##### Active Member
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: