Android Question Distance in Km/h in 2 coordinates

Situ LLC

Active Member
Licensed User
Distance in Km/h in 2 coordinates
Another way to calculate the distance between 2 coordinates, is very accurate.

Dim Distancias2 As Double

Dim lat As String = $"lat=${Location1.Latitude}"$
Dim lng As String = $"lon=${Location1.Longitude}"$

This ones source to compared
<Lat2s, Lon2s>

Distancias2= Haversine( lat, lng , Lat2s, Lon2s)



coordinates GPS:
rivate Sub Haversine(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double) As Double
    ' Convert latitude and longitude from degrees to radians
    Dim lat1Rad As Double = DegreesToRadians(lat1)
    Dim lon1Rad As Double = DegreesToRadians(lon1)
    Dim lat2Rad As Double = DegreesToRadians(lat2)
    Dim lon2Rad As Double = DegreesToRadians(lon2)
 
    ' Haversine formula
    Dim dlon As Double = lon2Rad - lon1Rad
    Dim dlat As Double = lat2Rad - lat1Rad
    Dim a As Double = Power(Sin(dlat / 2), 2) + Cos(lat1Rad) * Cos(lat2Rad) * Power(Sin(dlon / 2), 2)
    Dim c As Double = 2 * ATan2(Sqrt(a), Sqrt(1 - a))

    [B]'  the result changing  radiusOfEarth by   6371 km , 3959 millas or  Meters 6717407.00 [/B]

    Dim radiusOfEarth As Double = 6371 ' Earth radius in kilometers
    Dim distance As Double = radiusOfEarth * c
 
    Return distance
End Sub

Private Sub DegreesToRadians(degrees As Double) As Double
    Dim Pi As Double = 3.141592653589793238462643
    Return degrees * (Pi / 180)
End Sub

Enjoy
Regard
 
Last edited:
Top