Android Question How to calculate distance between 2 GPS coordinates?

incendio

Well-Known Member
Licensed User
Longtime User
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?
 

DonManfred

Expert
Licensed User
Longtime User
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.
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
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
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0
Top