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