Android Example Simple Compass

Have done this just for the fun of it. Have wrapped this posting. The B4A code is short and sweet:

B4X:
#Region  Project Attributes
    #ApplicationLabel: Compass
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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.

    Private c1 As Compass
    Private 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("main")

End Sub

Sub Activity_Resume
  
    c1.registerListener

End Sub

Sub Activity_Pause (UserClosed As Boolean)
  
    c1.unregisterListener

End Sub

Sub c1_current_orientation(orient As Int)
  
    Label1.Text = "Degrees from North = " & orient
  
End Sub

1.png


Library:

Compass
Author:
Author: Marko Gargenta, Wrapped by: Johan Schoeman
Version: 1
  • Compass
    Events:
    • current_orientation (orientation As Int)
    Fields:
    • ba As BA
    Methods:
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • onAccuracyChanged (sensor As Int, accuracy As Int)
    • onSensorChanged (sensor As Int, values() As Float)
    • registerListener
    • unregisterListener
    Properties:
    • Background As Drawable
    • Color As Int [write only]
    • Enabled As Boolean
    • Height As Int
    • Left As Int
    • Parent As Object [read only]
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int

Posting the B4A project, the B4A lib files, and the Java Code. Take note of the Objects/res/drawable folder of the B4A project - it contains the "compass rose".

Place your device on a flat surface and then turn it CW / ACW.
 

Attachments

  • TheJavaCode.zip
    37.4 KB · Views: 991
  • b4aCompass.zip
    105.8 KB · Views: 1,322
  • CompassLibFiles.zip
    6.5 KB · Views: 1,339

Johan Schoeman

Expert
Licensed User
Longtime User

mjcoon

Well-Known Member
Licensed User
It's very pretty. But if I put it flat on my desk and rortate it clockwise the compass rose moves further clockwise instead of compensating for the movement and remaining pointing in the same direction, as compass would.

Is this because you have assumed I am also in the southern hemisphere? ;-)

Mike.
 

mjcoon

Well-Known Member
Licensed User
Have done this just for the fun of it. Have wrapped this posting. The B4A code is short and sweet:

B4X:
#Region  Project Attributes
    #ApplicationLabel: Compass
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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.

    Private c1 As Compass
    Private 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("main")

End Sub

Sub Activity_Resume
 
    c1.registerListener

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
    c1.unregisterListener

End Sub

Sub c1_current_orientation(orient As Int)
 
    Label1.Text = "Degrees from North = " & orient
 
End Sub

View attachment 43082

Library:

Compass
Author:
Author: Marko Gargenta, Wrapped by: Johan Schoeman
Version: 1
  • ...

Posting the B4A project, the B4A lib files, and the Java Code. Take note of the Objects/res/drawable folder of the B4A project - it contains the "compass rose".

Place your device on a flat surface and then turn it CW / ACW.

It's very pretty. But if I put it flat on my desk and rotate it clockwise the compass rose moves further clockwise instead of compensating for the movement and remaining pointing in the same direction, as a compass would.

Is this because you have assumed I am also in the southern hemisphere? ;-)

Mike.

Reply
 
Top