Android Question Combining GPS & Compass to Point USER at specific location

Shahid Saeed

Active Member
Licensed User
Longtime User
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
 

Shahid Saeed

Active Member
Licensed User
Longtime User
Your project requirements are actually more complicated.

In GPS_LocationChanged event you can easily calculate the direction to the target with Location.BearingTo. Point to the north and test it.
When you get this step correctly you need adjust the direction based on the current orientation.
Sorry for My Limited Knowledge: I have done what you said but still it is not accurate. I am using two needles one to set the north position so the second will show the direction accordingly.

B4X:
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
    Dim QiblaDir As Float
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    'Longtitude 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 - QiblaDir
    lblLat.Text = dblAngle
    lblLon.Text = dblAngleQ   
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    Dim Qibla As Location
    Qibla.Initialize2(21.422497, 39.826179)
    QiblaDir =    Location1.BearingTo(Qibla)
    'DLat = Location1.Latitude
    'DLon = Location1.Longitude   
End Sub
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
@Erel I was testing the APP indoors and when I tried it outdoor it worked. But now my question is if the app is being used indoors it is keeping the value 0.0. How can I get the location values from Internet which is more likely supported in Android. Is there a possibility?
 
Upvote 0
Top