# Android QuestionHow to calculate distance between 2 GPS coordinates?

#### incendio

##### Well-Known Member
Hi guys,

I'm newbie in this GPS things.

Suppose, there's 2 coodinates, A & B.
A : LAT 35:24, LON 36:20
B : LAT 30:10, LON 25:04

How to calculate distance in meters between A & B?

#### KZero

##### Active Member

• incendio

#### DonManfred

##### Expert
Suppose, there's 2 coodinates, A & B.
A : LAT 35:24, LON 36:20
B : LAT 30:10, LON 25:04

How to calculate distance in meters between A & B?

You can create two Location objects (GPS library) and then calculate the distance with Location.DistanceTo.

#### harinder

##### Active Member
Add GPS library in Libraries Manager
B4X:
``````    Dim Location1, Location2 As Location
Location1.Initialize
Location2.Initialize
Dim distance As Int
Location1.Latitude = 35.24
Location1.Longitude = 36.20
Location2.Latitude = 30.10
Location2.Longitude = 25.04
distance = Location1.DistanceTo(Location2)    'distance in meters
distance=distance*.00054                      'distance in nautical miles``````

• incendio

#### Alex_197

##### Active Member
CalcDistance:
``````Public Sub CalcDistance(CurrentLat As Double,CurrentLong As Double,HouseLat As Double,HouseLong As Double) As Double

Try

Dim LatP As Double=CurrentLat
Dim LongP As Double=CurrentLong
Dim LatD As Double=HouseLat
Dim LongD As Double=HouseLong

Dim Miles As Double
Dim Yards As Double
Dim X As Double

Dim Coeff As Double= 1760.0

X = Sin(LatP * 0.01745) * Sin(LatD * 0.01745) + Cos(LatP * 0.01745)* Cos(LatD * 0.01745) * Cos(( LongD - LongP ) * 0.01745)

Miles = 1.15 * 3963 * ( ATan(-X / Sqrt(-X * X+ 1.0000000000001)) + 2* ATan(1) )

Miles = Round2(Miles, 2)

Yards = Miles * Coeff

Return Round( Yards)

Catch

Log("CalcDistance " & LastException)
ShowError("CalcDistance " & LastException)
Return -1
End Try

End Sub``````

• Jeffrey Cameron and incendio