Android Question How to measure distance with GPS?

Sapta

Member
Licensed User
Longtime User
Hi all,

In accordance with this example (GPS Tutorial), I want to calculate the distance
Using the additional code below why not accurate to measure the distance?
I compare with other applications, SpeedoMeter is more accurate. Is my code something wrong?

Please help me.

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: GPS
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
#End Region
#BridgeLogger: true
Sub Process_Globals
    
End Sub

Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
    Private LblJarak As Label
    Private StartLocation, EndLocation         As Location
    Private LatitudeReal, LongitudeReal     As Double
    Private LatitudeStart, LongitudeEnd     As Double
    Private Distance                         As Int
    Private StartClick As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    Distance = 0
    StartClick = False
End Sub

Sub Activity_Resume
    If Starter.GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then CallSubDelayed(Starter, "StartGPS")
    End If
End Sub
    

Sub Activity_Pause (UserClosed As Boolean)
    CallSubDelayed(Starter, "StopGPS")
End Sub

Public Sub GpsStatus (Satellites As List)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("Satellites:").Append(CRLF)
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite = Satellites.Get(i)
        sb.Append(CRLF).Append(Satellite.Prn).Append($" $1.2{Satellite.Snr}"$).Append(" ").Append(Satellite.UsedInFix)
        sb.Append(" ").Append($" $1.2{Satellite.Azimuth}"$).Append($" $1.2{Satellite.Elevation}"$)
    Next
    lblSatellites.Text = sb.ToString
End Sub

Public Sub LocationChanged(Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text = $"Speed = $1.2{Location1.Speed} m/s "$
    
    LatitudeReal   = Location1.Latitude
    LongitudeReal  = Location1.Longitude
    
    If StartClick = True Then
        EndLocation.Initialize2(LatitudeReal, LongitudeReal)
        Distance = Distance + StartLocation.DistanceTo(EndLocation)
        Log(Distance)  'Why not 
        
        'Reset to start
        LatitudeStart      = LatitudeReal
        LongitudeEnd     = LongitudeReal
        StartLocation.Initialize2(LatitudeStart, LongitudeEnd)
    End If
End Sub
    
Sub BtnStart_Click
    StartClick = True
    StartLocation.Initialize2(LatitudeReal, LongitudeReal)
    EndLocation.Initialize2(LatitudeReal, LongitudeReal)
End Sub
 

derez

Expert
Licensed User
Longtime User
The location is changing all the time, small jumps, even if you don't move. If you reset startpoint every time - you are accumulating the jumps distance.
If you want to measure the distance from start to end in straight line - measure it once after defining endpoint.
If you want to measure distance along the way - use your sub but do the resets and addition of distance only if the distance is greater than usual jump.
upload_2018-4-6_8-9-2.png
 
Upvote 0

Sapta

Member
Licensed User
Longtime User
Hi @derez thank you for your answer.
I choose this option bellow, is there any change in my code? because if I use my code, the distance reading is not accurate. Any idea?

"If you want to measure distance along the way - use your sub but do the resets and addition of distance only if the distance is greater than usual jump."

I use map for tracking run app.
Thanks
 
Upvote 0

derez

Expert
Licensed User
Longtime User
B4X:
EndLocation.Initialize2(LatitudeReal, LongitudeReal)
If StartClick = True and StartLocation.DistanceTo(EndLocation) > 15  Then
        Distance = Distance + StartLocation.DistanceTo(EndLocation)
        Log(Distance)  'Why not
       
        'Reset to start
        LatitudeStart      = LatitudeReal
        LongitudeEnd     = LongitudeReal
        StartLocation.Initialize2(LatitudeStart, LongitudeEnd)
    End If
 
Upvote 0
Top