Android Code Snippet [B4X] Find distance between two points (Lat/Lon)

Here is a function that could be useful if you want to determine the distance between two coordinates. The distance is returned in Miles.

B4X:
'
' Return Distance between two points
'
Sub distance(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double) As Double
    Dim theta As Double
    Dim dist As Double
   
    theta = lon1 - lon2
    dist = Sin(deg2rad(lat1)) * Sin(deg2rad(lat2)) + Cos(deg2rad(lat1)) * Cos(deg2rad(lat2)) * Cos(deg2rad(theta))
    dist = ACos(dist)
    dist = rad2deg(dist)
    Return dist * 60 * 1.1515
End Sub

'
'  This function converts decimal degrees to radians
'
Sub deg2rad(Deg As Double) As Double
    Dim pi As Double= 3.14159265358979
    Return Deg * pi / 180
End Sub

'
'  This function converts radians to decimal degrees
'
Sub rad2deg(Rad As Double) As Double
    Dim pi As Double= 3.14159265358979
    Return Rad * 180 / pi
End Sub
 

derez

Expert
Licensed User
Longtime User
Can be done with GPS library :
Location
A Location object holds various information about a specific GPS fix.
In most cases you will work with locations that are passed to the GPS LocationChanged event.
The location object can also be used to calculate distance and bearing to other locations.
 

klaus

Expert
Licensed User
Longtime User
It seems that you don't know that B4A has trigonometic functions for degrees!
No need to tranform degrees to radians!
SinD, CosD, TanD, ACosD, ASinD, ATanD.
Insted of Sin(deg2rad(lat1)) use simply SinD(lat1)

It seems that you don't know either that B4A has an internal constant for Pi: cPi
 

aminoacid

Active Member
Licensed User
Longtime User
Yes... I'm learning new things every day.:) Thanks for the feedback. I ported this over from a old VB6 Application to my B4X application since I needed this capability with the GoogleMaps library already loaded and did not want to have to load another Library (GPS) just for one simple calculation.
 

aminoacid

Active Member
Licensed User
Longtime User
Much more compact ......

B4X:
'
' Return Distance between two points in miles
'
Sub distance(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double) As Double
    Dim theta As Double
    Dim dist As Double
    Dim dist2 As Double
    theta = lon1 - lon2
    dist = SinD(lat1) * SinD(lat2) + CosD(lat1) * CosD(lat2) * CosD(theta)
    dist = ACosD(dist)
    Return dist * 60 * 1.1515
End Sub
 

mw71

Active Member
Licensed User
Longtime User
Distance and direction between 2 Maidenhead Locator, but uses internal Lat / Lon information

B4X:
Sub Loc2Dis (LocVStr As String, LocZStr As String) As Map

   
    Dim r As Int = 6371.137
    Dim rad2deg As Double = 180.0 / cPI
   
    Dim LocV, LocZ As Map
   
    LocV.Initialize
    LocZ.Initialize
   
    LocV=Loc2LatLon(LocVStr)
    LocZ=Loc2LatLon(LocZStr)  
       
    Dim B1 As Double = LocV.Get("b")
    Dim B2 As Double = LocZ.Get("b")
    Dim L1 As Double = LocV.Get("l")
    Dim L2 As Double = LocZ.Get("l")

   
    Dim ew As Double             = ACos(SinD(B1) * SinD(B2) + CosD(B1) * CosD(B2) * CosD(L1-L2))    'entfernung in Winkelangabe
    Dim Entfernung As Double    = r * ew                                                        'Umrechnung in Längenangabe

    Dim tc As Double
    If Sin(L2-L1)>=0 Then
        tc=ACos((SinD(B2) - SinD(B1) * Cos(ew)) / (Sin(ew) * CosD(B1)))
    Else
        tc=(2*cPI) - ACos((SinD(B2) - SinD(B1) * Cos(ew)) / (Sin(ew) * CosD(B1)))
    End If

    Dim Richtung1 As Double = (tc * rad2deg * 10) / 10

    Log("Entfernung  " & NumberFormat(Entfernung,1,1))
    Log("Richtung1   " & NumberFormat(Richtung1,1,1))

    Dim MapR As Map
    MapR.Initialize
    MapR.Put("E",NumberFormat(Entfernung,1,0))  'Dist
    MapR.Put("R",NumberFormat(Richtung1,1,0))   'Dir
    Return MapR
End Sub

Sub Loc2LatLon(locator As String) As Map
   
    Dim l1, l2, l3 As Double
    Dim b1, b2, b3 As Double
    Dim LocTrenn(locator.Length) As String

    For i=0 To locator.Length-1
        LocTrenn(i)=locator.ToUpperCase.SubString2(i,i+1)
    Next

    For i=0 To 17
        If LocTrenn(0)= Chr(65+i) Then l1=-180 + (i*20)
    Next

    For i=0 To 17
        If LocTrenn(1)= Chr(65+i) Then b1=-90 + (i*10)
    Next

    For i=0 To 9
        If LocTrenn(2)= i Then l2= i*2
    Next

    For I=0 To 9
        If LocTrenn(3)= I Then b2=i
    Next
   
    For i=0 To 23
        If LocTrenn(4)= Chr(65+i) Then l3= (i*2)/24
    Next
   
    For i=0 To 23
        If LocTrenn(5)= Chr(65+i) Then b3= i/24
    Next

    Dim l As Double = l1+l2+l3
    Dim b As Double = b1+b2+b3
   
    Dim retM As Map
    retM.Initialize
    retM.Put("l",l)
    retM.Put("b",b)
   
    Return retM
End Sub
 
Top