B4J Question Distance measurement of 2 GPS positions with B4J

V.Wolpers

Member
Hello everyone,

I wrote a small program to measure the distance of 2 GPS coordinates.
As a B4A version it works perfectly.
How do I have to rewrite this for B4J because there is no package android.location?

I am attaching both as a zip file.

Thanks for your help.
 

Attachments

  • GPS_Dist_Android.zip
    3.7 KB · Views: 49
  • GPS_Dist-Java.zip
    2.6 KB · Views: 51

emexes

Expert
Licensed User
measure the distance of 2 GPS coordinates.

as in to calculate the distance between two points on surface or earth?

(usually specified as longitude+latitude, but not necessarily from a GPS)

The navigation library mentioned above looks like it will do your job, but if you want to see how it works and/or roll your own, then check out:

Aviation Formulary V1.47 By Ed Williams

Lol on a related note... I still remember reading the notes at the back of this book, where the author/pilot was talking about calculating fuel loads, and mentioned that planes weigh less when travelling east (and thus less fuel is required).

1679648061820.png
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Aviation Formulary V1.47 By Ed Williams
B4X:
'This sub calculates the distance and course between two Lat/Lon coordinates.
'The formulas are based on this site: http://williams.best.vwh.net/avform.htm (Ed Williams)

Sub DistanceCourse
    Dim lat1 As Double = Target.Latitude
    Dim lon1 As Double = Target.Longitude
    Dim lat2 As Double = MeI.Latitude 'my position
    Dim lon2 As Double = MeI.Longitude 'my position
    Try
    lat1 = lat1 * cPI / 180
    lon1 = -lon1 * cPI / 180
    lat2 = lat2 * cPI / 180
    lon2 = -lon2 * cPI / 180
    Dim d As Double = 2 * ASin(Sqrt(Power(Sin((lat1-lat2)/2),2) + Cos(lat1)*Cos(lat2)*Power(Sin((lon1-lon2)/2),2)))
    Dim Dist As Double = d * 180 * 60 * 1852/cPI
    Dim tc1 As Double
    If Cos(lat1) < 1e-7 Then
        If (lat1 > 0) Then
            tc1 = cPI
        Else
            tc1= 2*cPI
        End If
    Else
        sn = Sin(lon2-lon1)
        If Abs(sn) < 1e-7 Then
            If lat1 > lat2 Then tc1 = cPI Else tc1 = 2*cPI
        Else If sn < 0 Then     
            tc1=ACos((Sin(lat2)-Sin(lat1)*Cos(d))/(Sin(d)*Cos(lat1))) 
        Else   
            tc1=2*cPI-ACos((Sin(lat2)-Sin(lat1)*Cos(d))/(Sin(d)*Cos(lat1)))
        End If
    End If

    Course = tc1 * 180 / cPI

    'calculating decimal X,Y
    'x=Dist* Cos(tc1-cPI/2)
    'y=Dist* Sin(tc1-cPI/2)
    Return

    Catch
      Dist = 0
      Course = 0
    End Try

End Sub
 
Upvote 0
Top