I am trying to use GPS & Compass to direct user towards a specif location from his current location, But I am not getting the accurate direction, I am sure I am doing something wrong. Below is the code to see what I am doing:-
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Orientation As PhoneOrientation
Dim tmrUpdater As Timer
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 pnlRose As Panel
Dim Rotation As Animation
Dim RotationQ As Animation
Dim dblAngle As Double
Dim dblAngleQ As Double
Dim pnlNeedle As Panel
Dim pnlQibla As Panel
Dim PageTitle As Label
Dim lblLat As Label
Dim lblLon As Label
Dim DLat, DLon As Float
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
GPS1.Initialize("GPS")
End If
End Sub
Sub Activity_Resume
If GPS1.GPSEnabled = False Then
If Msgbox2("Allow Application to use your current location", "GPS", "Yes allow", "", "Don't allow", LoadBitmap(File.DirAssets,"icon.png")) = DialogResponse.POSITIVE Then
StartActivity(GPS1.LocationSettingsIntent)
End If
Else
GPS1.Start(0,0)
End If
dblAngle = 0
dblAngleQ = 0
Dim BG As Bitmap = LoadBitmapSample(File.DirAssets, "bg.jpg", Activity.Width, Activity.Height)
fnc.SetTiledBackground(Activity, BG)
Activity.LoadLayout("qibla")
PageTitle.Text = "Qibla Direction"
pnlRose.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "compass.png", pnlRose.Width, pnlRose.Width))
pnlNeedle.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "needle.png", pnlRose.Width/9.6, pnlRose.Width))
pnlQibla.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "qibla.png", pnlRose.Width/9.6, pnlRose.Width))
Rotation.InitializeRotateCenter("Animation", dblAngle, dblAngle, pnlNeedle)
RotationQ.InitializeRotateCenter("AnimationQ", dblAngleQ, dblAngleQ, pnlQibla)
Rotation.Duration = 20000
RotationQ.Duration = 20000
tmrUpdater.Initialize("tmrUpdater",100)
Orientation.StartListening("Orientation")
tmrUpdater.Enabled = True
End Sub
Sub Activity_Pause (UserClosed As Boolean)
GPS1.Stop
Dim bmpClear As Bitmap
bmpClear.InitializeMutable(1,1)
pnlRose.SetBackgroundImage(bmpClear)
pnlNeedle.SetBackgroundImage(bmpClear)
pnlQibla.SetBackgroundImage(bmpClear)
End Sub
Sub Animation_AnimationEnd
Try
Rotation.Start(pnlNeedle)
RotationQ.Start(pnlQibla)
Catch
Log(LastException.Message)
End Try
End Sub
Sub Orientation_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float)
Dim QLat As Double = 21.422497 'Latitude of the destination
Dim QLon As Double = 39.826179 'Longtitute of the destination
Dim lonDelta As Float = (DLon - QLon)
Dim y As Float = Sin(lonDelta) * Cos(DLat)
Dim x As Float = Cos(QLat) * Sin(DLat) - Sin(QLat) * Cos(DLat) * Cos(lonDelta)
Dim qibla As Float = Round(ATan2(y, x))
dblAngle = -Azimuth
dblAngleQ = Azimuth - qibla
lblLat.Text = dblAngle
lblLon.Text = dblAngleQ
End Sub
Sub tmrUpdater_Tick
Rotation.InitializeRotateCenter("Animation", dblAngle, dblAngle, pnlNeedle)
RotationQ.InitializeRotateCenter("AnimationQ", dblAngleQ, dblAngleQ, pnlQibla)
Rotation.Duration = 20000
RotationQ.Duration = 20000
Rotation.Start(pnlNeedle)
RotationQ.Start(pnlQibla)
End Sub
Sub GPS_LocationChanged (Location1 As Location)
DLat = Location1.Latitude
DLon = Location1.Longitude
End Sub
Sub GPS_UserEnabled (Enabled As Boolean)
ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub