Android Code Snippet Convert GPS Location1.Bearing to readable string

The following code converts location1.bearing to readable string

Example : Dim MyDirection as String
MyDirection = GetCompassDirection(location1.bearing)


B4X:
Sub GetCompassDirection(bearing As Double) As String
     
        Dim tmp As Int
      ' Set Default Direction
        Dim direction As String : direction = "N"
               
        tmp = Floor(Round(bearing / 22.5))
        Select Case tmp
            Case 1
                direction = "NNE"

            Case 2
                direction = "NE"

            Case 3
                direction = "ENE"

            Case 4
                direction = "E"

            Case 5
                direction = "ESE"

            Case 6
                direction = "SE"

            Case 7
                direction = "SSE"

            Case 8
                direction = "S"

            Case 9
                direction = "SSW"

            Case 10
                direction = "SW"

            Case 11
                direction = "WSW"

            Case 12
                direction = "W"

            Case 13
                direction = "WNW"

            Case 14
                direction = "NW"

            Case 15
                direction = "NNW"

            Case Else
                direction = "N"


        End Select
      Return direction

    End Sub

Tag: Direction , GPS direction , GPS Heading
 

Beja

Expert
Licensed User
Longtime User
Hi Reviewnow,
I got this error:

B4A version 4.30
Parsing code. Error
Error parsing program.
Error description: Syntax error.
Occurred on line: 45
Sub GetCompassDirection(bearing As Double) As String

any explanation appreciated.
 

Reviewnow

Active Member
Licensed User
Longtime User
Beja,
Possible to show how you are calling the sub Procedure
 

Reviewnow

Active Member
Licensed User
Longtime User
Here is example code works perfectly fine
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
Dim GPS1 As GPS
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Dim label1 As Label

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
If FirstTime Then


Log(GetCompassDirection(0))
Log(GetCompassDirection(0.1982323))
Log(GetCompassDirection(6.9381305))
Log(GetCompassDirection(7.929292))
Log(GetCompassDirection(8.1275243))
Log(GetCompassDirection(9.5151504))
Log(GetCompassDirection(10.1098473))
Log(GetCompassDirection(11.1010088))
Log(GetCompassDirection(21.627990923))
Log(GetCompassDirection(31.956740746))
Log(GetCompassDirection(42.285490569))
Log(GetCompassDirection(52.614240392))
Log(GetCompassDirection(62.942990215))
Log(GetCompassDirection(73.271740038))
Log(GetCompassDirection(83.600489861))
Log(GetCompassDirection(93.929239684))
Log(GetCompassDirection(104.257989507))
Log(GetCompassDirection(114.58673933))
Log(GetCompassDirection(124.915489153))
Log(GetCompassDirection(135.244238976))
Log(GetCompassDirection(145.572988799))
Log(GetCompassDirection(155.901738622))
Log(GetCompassDirection(166.230488445))
Log(GetCompassDirection(176.559238268))
Log(GetCompassDirection(186.887988091))
Log(GetCompassDirection(197.216737914))
Log(GetCompassDirection(207.545487737))
Log(GetCompassDirection(217.87423756))
Log(GetCompassDirection(228.202987383))
Log(GetCompassDirection(238.531737206))
Log(GetCompassDirection(248.860487029))
Log(GetCompassDirection(259.189236852))
Log(GetCompassDirection(269.517986675))
Log(GetCompassDirection(279.846736498))
Log(GetCompassDirection(290.175486321))
Log(GetCompassDirection(300.504236144))
Log(GetCompassDirection(310.832985967))
Log(GetCompassDirection(321.16173579))
Log(GetCompassDirection(331.490485613))
Log(GetCompassDirection(341.819235436))
Log(GetCompassDirection(352.147985259))
Log(GetCompassDirection(362.476735082))
Log(GetCompassDirection(372.805484905))

GPS1.Initialize("GPS")

End If
   
label1.Initialize("label1")
label1.Text = "Waiting Gps Update"
label1.TextSize = 22
label1.TextColor = Colors.Blue
label1.Color = Colors.White

Activity.AddView(label1,30dip,60dip,230dip,40dip)
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 GetCompassDirection(bearing As Double) As String
    
        Dim tmp As Int
      ' Set Default Direction
        Dim direction As String : direction = "N"
              
        tmp = Floor(Round(bearing / 22.5))
        Select Case tmp
            Case 1
                direction = "NNE"

            Case 2
                direction = "NE"

            Case 3
                direction = "ENE"

            Case 4
                direction = "E"

            Case 5
                direction = "ESE"

            Case 6
                direction = "SE"

            Case 7
                direction = "SSE"

            Case 8
                direction = "S"

            Case 9
                direction = "SSW"

            Case 10
                direction = "SW"

            Case 11
                direction = "WSW"

            Case 12
                direction = "W"

            Case 13
                direction = "WNW"

            Case 14
                direction = "NW"

            Case 15
                direction = "NNW"

            Case Else
                direction = "N"


        End Select
      Return direction

    End Sub
    Sub GPS_LocationChanged (Location1 As Location)
'Get the current gps direction
   Log("Bearing:" &Location1.Bearing)
   Log("Direction:" & GetCompassDirection(Location1.Bearing))
   label1.Text = GetCompassDirection(Location1.Bearing)
End Sub

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

Beja

Expert
Licensed User
Longtime User
Thanks Reviewnow,
working great now. .
 

DonManfred

Expert
Licensed User
Longtime User

Beja

Expert
Licensed User
Longtime User
Hi Don,
I don't think I saved that test app.. in fact I placed the last code that reviewnow posted over it..
If recall it from my memory then the result is not very good.
 

Serge Bertet

Active Member
Licensed User
Thank you for this snippet!
Even if it is an old post, here is a short way to display directions.

B4X:
Sub Globals
    ...
    Private dirs() As String = Array As String ("Nord", "N.N.E", "N.E", "E.N.E", "Est", "E.S.E", "S.E", "S.S.E", "Sud", "S.S.O", "S.O", "O.S.O", "Ouest", "O.N.O", "N.O", "N.N.O", "Nord")
End Sub

Private Sub GPSEvent_LocationChanged(Loc As Location)
    If Loc.BearingValid Then
        Boussole.Text = "Compas : cap " & dirs(Floor(Round(Loc.bearing / 22.5)))
End Sub
 
Top