GPS running count of distance

anaylor01

Well-Known Member
Licensed User
Longtime User
I am using the example from the forum. I am trying to create a simple app to keep a running distance from a starting point. Below is the code I have. The problem is the distance calculation. getDistanceKM. I would also like to change this to MILES. Any help would be appreciated.
B4X:
Sub Process_Globals
    Dim GPS1 As GPS
End Sub

Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
    Dim btnTurnOnGps As Button
    Dim latstart,latstop,lonstart,lonstop As String
    Dim lblDistance As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        GPS1.Initialize("GPS")
    End If
    Activity.LoadLayout("GPSMain")
End Sub

Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text = "Speed = " & Location1.Speed
    latstop = Location1.Latitude
    lonstop =Location1.Longitude
    getdistanceKM(latstart,lonstart,latstop,lonstop)
    latstart = latstop
    lonstart = lonstop

End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
lblsatellites.Initialize("")
    lblSatellites.Text = "Satellites:" & CRLF
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite
        Satellite = Satellites.Get(i)
        lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
            " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
            & " " & Satellite.Elevation
    Next
    
End Sub
Sub btnTurnOnGps_click
Dim location1 As Location
Location1.Initialize
If btnturnongps.Text = "Start" Then
btnTurnOnGps.Text = "Stop"
latstart =  Location1.Latitude
lonstart = Location1.Longitude
Else
btnTurnOnGps.Text = "Start"
getdistanceKM(latstart,lonstart,latstop,lonstop)
End If

End Sub
Sub getDistanceKM(lat1, long1, lat2, long2)

    DEGREES_TO_RADIANS = (cPI / 180.0)
    EARTH_RADIUS = 6371.0
    
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2
    
    '    There is no real reason To break this lot into 
    '    4 statements but I just feel it's a little more 
    '    readable.
    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = Sin(rlat1) * Sin(rlat2)

    ret = p1 + p2 + p3
    If ret = 1 Then Return 0
    
    ret = ACos(ret)
    ret = ret * EARTH_RADIUS
    lbldistance.text = ret
    Return ret
    
End Sub
 

anaylor01

Well-Known Member
Licensed User
Longtime User
I am still having a problem getting the distance to add up correctly. I saw in the post that someone is using an accelometer. Is using gps the best way?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I am still having a problem getting the distance to add up correctly.
What exactly do you want to do ?

I saw in the post that someone is using an accelometer.
An accelerometer is of no use to determine a disrance.

Is using gps the best way?
Again, what exactly do you want to do ?

Best regards.
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I am trying to create a pedometer. When the user clicks the start button it will start calculating distance. Here is how I am trying to calculate the distance.
B4X:
Sub btnTurnOnGps_click
Dim location1 As Location
Location1.Initialize

If btnturnongps.Text = "Start" Then
    'btnturnongps.Initialize("")
    btnTurnOnGps.Text = "Stop"
    btnTurnOnGps.TextColor = Colors.white
    cols1(0) = Colors.Red
    cols1(1) = Colors.Red
    gd1.Initialize("TOP_BOTTOM",cols1)
    gd1.CornerRadius = 11
    btnturnongps.Background = gd1
    GPS1.Start(0, 0) 'Listen to GPS with no filters.

    latstart =  Location1.Latitude
    lonstart = Location1.Longitude
Else
    btnTurnOnGps.Text = "Start"
    btnTurnOnGps.TextColor = Colors.Black
    gps1.Stop
    'btnturnongps.Initialize( Colors.RGB(124, 252, 0),CornerRadius)
    btnturnongps.Color = Colors.RGB(124, 252, 0)
    getdistanceKM(latstart,lonstart,latstop,lonstop)
End If

End Sub

B4X:
Sub getDistanceKM(lat1, long1, lat2, long2)

    DEGREES_TO_RADIANS = (cPI / 180.0)
    EARTH_RADIUS = 6371.0
    'Msgbox(lat1,"lat1")
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2

    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = Sin(rlat1) * Sin(rlat2)

    ret = p1 + p2 + p3
    If ret = 1 Then Return 0
    
    ret = ACos(ret)
    ret = (ret * EARTH_RADIUS) * 062
    'Msgbox(ret,"RET")
distance = ret + distance
'Msgbox(distance,"Dist")
lbldistance.text = distance
    
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would do like below:
B4X:
Sub Process_Globals
    Dim GPS1 As GPS
    Dim Distance As Double
End Sub
 
Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
    Dim btnTurnOnGps As Button
    Dim latstart,latstop,lonstart,lonstop As String
    Dim lblDistance As Label
End Sub
 
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        GPS1.Initialize("GPS")
    End If
    Activity.LoadLayout("GPSMain")
End Sub
 
Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub
 
Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
End Sub
 
Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text = "Speed = " & Location1.Speed
    latstop = Location1.Latitude
    lonstop =Location1.Longitude
    Distance = Distance + getdistanceKM(latstart,lonstart,latstop,lonstop)
    latstart = latstop
    lonstart = lonstop
End Sub
 
Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub
 
Sub GPS_GpsStatus (Satellites As List)
lblsatellites.Initialize("")
    lblSatellites.Text = "Satellites:" & CRLF
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite
        Satellite = Satellites.Get(i)
        lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
            " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
            & " " & Satellite.Elevation
    Next  
End Sub
 
Sub btnTurnOnGps_click
  Dim location1 As Location
  Location1.Initialize
  If btnturnongps.Text = "Start" Then
    btnTurnOnGps.Text = "Stop"
    latstart =  Location1.Latitude
    lonstart = Location1.Longitude
    Distance = 0
  Else
    btnTurnOnGps.Text = "Start"
    getdistanceKM(latstart,lonstart,latstop,lonstop)
'    lblDistance.Text = Distance  ' in km
    lblDistance.Text = Distance * 0.621 ' in miles
  End If
End Sub
 
Sub getDistanceKM(lat1, long1, lat2, long2)
    DEGREES_TO_RADIANS = (cPI / 180.0)
    EARTH_RADIUS = 6371.0
 
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2
 
    '    There is no real reason To break this lot into 
    '    4 statements but I just feel it's a little more 
    '    readable.
    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = Sin(rlat1) * Sin(rlat2)
    ret = p1 + p2 + p3
    If ret = 1 Then Return 0
 
    ret = ACos(ret)
    ret = ret * EARTH_RADIUS
    Return ret
 
End Sub

Not tested, I don't have a device (yet).

Best regards.
 
Last edited:
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I am getting an numberFormatException on.
rlat1=DEGREES_TO_radians * lat1.
I am guessing it doesn't like:
Distance = Distance + getdistanceKM(latstart,lonstart,latstop,lonstop)
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Still getting number format exception on.
B4X:
rlat1 = DEGREES_TO_RADIANS * lat1

B4X:
Sub Process_Globals
    Dim GPS1 As GPS
    Dim Distance As Double
End Sub
 
Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
    Dim btnTurnOnGps As Button
    Dim latstart,latstop,lonstart,lonstop As String
    Dim lblDistance As Label
    Dim layoutVal As LayoutValues
    Dim lblDistanceLabel As Label
    Dim gd1 As GradientDrawable
  Dim gd2 As GradientDrawable
  Dim cols1(2) As Int
  Dim cols2(2) As Int
End Sub
 
Sub Activity_Create(FirstTime As Boolean)
   ' If FirstTime Then
    '    GPS1.Initialize("GPS")
   
    Activity.LoadLayout("GPSMain")
    btnTurnOnGps.Initialize("")
    lblDistanceLabel.Initialize("")
    'btnTurnOnGps.Top = layoutval.Height - btnTurnOnGps.Height '-(btnTurnOnGps.Height/2)
    'lblDistanceLabel.Width = layoutval.Width
    ' End If
End Sub
 
Sub Activity_Resume
'    If GPS1.GPSEnabled = False Then
'        ToastMessageShow("Please enable the GPS device.", True)
'        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
'    Else
'        GPS1.Start(0, 0) 'Listen to GPS with no filters.
'    End If
End Sub
 
Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
End Sub
 
Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text = "Speed = " & (Location1.Speed * 3.2808399)
    latstop = Location1.Latitude
    lonstop =Location1.Longitude
    Distance = Distance + getdistanceKM(latstart,lonstart,latstop,lonstop)
    latstart = latstop
    lonstart = lonstop
End Sub
 
Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub
 
Sub GPS_GpsStatus (Satellites As List)
lblsatellites.Initialize("")
    lblSatellites.Text = "Satellites:" & CRLF
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite
        Satellite = Satellites.Get(i)
        lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
            " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
            & " " & Satellite.Elevation
    Next  
End Sub
 
Sub btnTurnOnGps_click
GPS1.Initialize("GPS")
  Dim location1 As Location
  Location1.Initialize
  If btnturnongps.Text = "Start" Then
   btnTurnOnGps.Text = "Stop"
   lbldistance.Initialize("")
    btnTurnOnGps.TextColor = Colors.white
    cols1(0) = Colors.Red
    cols1(1) = Colors.Red
    gd1.Initialize("TOP_BOTTOM",cols1)
    gd1.CornerRadius = 11
    btnturnongps.Background = gd1
    GPS1.Start(0, 0) 'Listen to GPS with no filters.

    latstart =  Location1.Latitude
    lonstart = Location1.Longitude
    Distance = 0
  Else
   btnTurnOnGps.Text = "Start"
    btnTurnOnGps.TextColor = Colors.Black
    gps1.Stop
    'btnturnongps.Initialize( Colors.RGB(124, 252, 0),CornerRadius)
    btnturnongps.Color = Colors.RGB(124, 252, 0)
    getdistanceKM(latstart,lonstart,latstop,lonstop)
'    lblDistance.Text = Distance  ' in km
    lblDistance.Text = Distance * 0.621 ' in miles
  End If
End Sub
 
Sub getDistanceKM(lat1, long1, lat2, long2) As Double
    DEGREES_TO_RADIANS = (cPI / 180.0)
    EARTH_RADIUS = 6371.0
 
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2
 
    '    There is no real reason To break this lot into 
    '    4 statements but I just feel it's a little more 
    '    readable.
    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = Sin(rlat1) * Sin(rlat2)
    ret = p1 + p2 + p3
    If ret = 1 Then Return 0
 
    ret = ACos(ret)
    ret = (ret * EARTH_RADIUS) 
    Return ret
 
End Sub
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim latstart,latstop,lonstart,lonstop As String
Should be
B4X:
    Dim latstart,latstop,lonstart,lonstop As Double
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I keep getting NaN for the distance. I am trying to a running total for distance.
B4X:
Sub Process_Globals
    Dim GPS1 As GPS
    Dim Distance As Double
End Sub
 Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
    Dim btnTurnOnGps As Button
    Dim latstart,latstop,lonstart,lonstop As Double
    Dim lblDistance As Label
    Dim layoutVal As LayoutValues
    Dim lblDistanceLabel As Label
    Dim gd1 As GradientDrawable
    Dim gd2 As GradientDrawable
    Dim cols1(2) As Int
    Dim cols2(2) As Int
    Dim lblSpeedLabel As Label
End Sub
 Sub Activity_Create(FirstTime As Boolean)
   ' If FirstTime Then
    GPS1.Initialize("GPS")
    Activity.LoadLayout("GPSMain")
    layoutVal=GetDeviceLayoutValues
    btnTurnOnGps.Top = layoutval.Height - btnTurnOnGps.Height -(btnTurnOnGps.Height/2)
    btnTurnOnGps.left = layoutval.Width /2 - (btnTurnOnGps.width/2)
    lblDistanceLabel.Width = layoutval.Width
    lblDistance.Width = layoutval.Width
    lblspeedlabel.Width = layoutval.Width
    lblspeed.Width = layoutval.Width
    lblspeed.Left = 0
    lblspeedlabel.Left = 0
    lblDistanceLabel.Left = 0
    lblDistance.Left = 0
    ' End If
End Sub
 Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub
 Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
End Sub
 Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text =NumberFormat((Location1.Speed / 3.2808399),0,2)
    latstop = Location1.Latitude
    lonstop = Location1.Longitude
    Distance = Distance + getdistanceKM(latstart,lonstart,latstop,lonstop)
    latstart = latstop
    lonstart = lonstop
    lblDistance.Text = NumberFormat(Distance * 0.621,0,2) ' in miles
End Sub
 Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub
Sub btnTurnOnGps_click
  Dim location1 As Location
  Location1.Initialize
  If btnTurnOnGps.Text = "Start" Then
        btnTurnOnGps.Text = "Stop"
        btnTurnOnGps.TextColor = Colors.white
        cols1(0) = Colors.Red
        cols1(1) = Colors.Red
        gd1.Initialize("TOP_BOTTOM",cols1)
        gd1.CornerRadius = 11
        btnturnongps.Background = gd1
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
        latstart =  Location1.Latitude
        lonstart = Location1.Longitude
        Distance = 0
  Else
        btnTurnOnGps.Text = "Start"
        btnTurnOnGps.TextColor = Colors.Black
        gps1.Stop
        btnturnongps.Color = Colors.RGB(124, 252, 0)
        getdistanceKM(latstart,lonstart,latstop,lonstop)
  End If
End Sub
 Sub getDistanceKM(lat1, long1, lat2, long2) As Double
    DEGREES_TO_RADIANS = (cPI / 180.0)
    EARTH_RADIUS = 6371.0
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2
    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = Sin(rlat1) * Sin(rlat2)
    ret = p1 + p2 + p3
    If ret = 1 Then Return 0
        ret = ACos(ret)
        ret = (ret * EARTH_RADIUS) 
    Return ret
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I suggest you to change the following:
B4X:
Sub getDistanceKM(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double
B4X:
GPS1.Start(0, 0) 'Listen to GPS with no filters.
latstart = 0
lonstart = 0
Distance = 0
B4X:
If lat1 = 0 and long1 = 0 then
 latstart = latstop
 longstart = longstop
End If 
Distance = Distance + getdistanceKM(latstart,lonstart,latstop,lonstop)
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Where do I put that last piece? Because Lat1 and Long1 is part of the Sub getDistanceKM(lat1, long1, lat2, long2) As Double SUB Routine.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, it should be:
B4X:
Sub GPS_LocationChanged (Location1 As Location)
.
.
   latstop = Location1.Latitude
   lonstop = Location1.Longitude
   If latstart = 0 and longstart = 0 then
     latstart = latstop
     longstart = longstop
   End If 
   Distance = Distance + getdistanceKM(latstart,lonstart,latstop,lonstop)
   latstart = latstop
   longstart = longstop
End Sub

Best regards.
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Thank you Klaus. I am not getting the error anymore but now it is reporting outrageous distance. 1133856.8812304384 and I am sitting still.
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Yeah. What I am calling it lonstart and lonstop and you were calling it long. I made the corrections but now I am back to NaN error.

B4X:
Sub Process_Globals
    Dim GPS1 As GPS
    Dim Distance As Double
End Sub
 Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
    Dim btnTurnOnGps As Button
    Dim latstart,latstop,lonstart,lonstop As Double
    Dim lblDistance As Label
    Dim layoutVal As LayoutValues
    Dim lblDistanceLabel As Label
    Dim gd1 As GradientDrawable
    Dim gd2 As GradientDrawable
    Dim cols1(2) As Int
    Dim cols2(2) As Int
    Dim lblSpeedLabel As Label
End Sub
 Sub Activity_Create(FirstTime As Boolean)
   ' If FirstTime Then
    GPS1.Initialize("GPS")
    Activity.LoadLayout("GPSMain")
    layoutVal=GetDeviceLayoutValues
    btnTurnOnGps.Top = layoutval.Height - btnTurnOnGps.Height -(btnTurnOnGps.Height/2)
    btnTurnOnGps.left = layoutval.Width /2 - (btnTurnOnGps.width/2)
    lblDistanceLabel.Width = layoutval.Width
    lblDistance.Width = layoutval.Width
    lblspeedlabel.Width = layoutval.Width
    lblspeed.Width = layoutval.Width
    lblspeed.Left = 0
    lblspeedlabel.Left = 0
    lblDistanceLabel.Left = 0
    lblDistance.Left = 0
    ' End If
End Sub
 Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
latstart = 0
lonstart = 0
Distance = 0
    End If
End Sub
 Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
End Sub
 Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text =NumberFormat((Location1.Speed / 3.2808399),0,2)
  latstop = Location1.Latitude
  lonstop = Location1.Longitude
    If latstart = 0 AND lonstart = 0 Then
      latstart = latstop
      lonstart = lonstop
    End If
  Distance = Distance + getdistanceKM(latstart,lonstart,latstop,lonstop)
  latstart = latstop
  lonstart = lonstop
    lblDistance.Text = Distance * 0.621 ' in miles
End Sub
 Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub
Sub btnTurnOnGps_click
  Dim location1 As Location
  Location1.Initialize
  If btnTurnOnGps.Text = "Start" Then
        btnTurnOnGps.Text = "Stop"
        btnTurnOnGps.TextColor = Colors.white
        cols1(0) = Colors.Red
        cols1(1) = Colors.Red
        gd1.Initialize("TOP_BOTTOM",cols1)
        gd1.CornerRadius = 11
        btnturnongps.Background = gd1
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
        latstart = 0
        lonstart = 0
        Distance = 0
        latstart =  Location1.Latitude
        lonstart = Location1.Longitude
        Distance = 0
  Else
        btnTurnOnGps.Text = "Start"
        btnTurnOnGps.TextColor = Colors.Black
        gps1.Stop
        btnturnongps.Color = Colors.RGB(124, 252, 0)
        'getdistanceKM(latstart,lonstart,latstop,lonstop)
  End If
End Sub
Sub getDistanceKM(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double
    DEGREES_TO_RADIANS = (cPI / 180.0)
    EARTH_RADIUS = 6371.0
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2
    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = Sin(rlat1) * Sin(rlat2)
    ret = p1 + p2 + p3
    If ret = 1 Then Return 0
        ret = ACos(ret)
        ret = (ret * EARTH_RADIUS) 
    Return ret
End Sub
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Hey Klaus, I am getting it in the following sub. It is returning something like1.950325013383847383E-4 or NaN.
B4X:
Sub getDistanceKM(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double
    DEGREES_TO_RADIANS = (cPI / 180.0)
    EARTH_RADIUS = 3959.0
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2
    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = Sin(rlat1) * Sin(rlat2)
    ret = p1 + p2 + p3
    If ret = 1 Then Return 0
      ret = ACos(ret)
      ret = (ret * EARTH_RADIUS) * 0.621
    Return ret
End Sub
 
Upvote 0
Top