Android Question [SOLVED] Indicating when you are facing the direction of a latitude and longitude

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I this possible with B4A?

This is the latitude and longitude for a particular location.

Place Name Latitude Longitude
Tewksbury, MA, USA 42.610649 -71.234222

This is the latitude and longitude for where I'm at.

Place Name Latitude Longitude
Lowell, MA, USA 42.640999 -71.316711

I would like to know if there is a B4A library that can tell me when I'm facing towards Tewksbury, MA from Lowell, MA. I plan to use the coding from Erel at https://www.b4x.com/android/forum/threads/orientation-and-accelerometer.6647/page-6#post-476271 to get my heading but don't know how to figure out when I'm facing Tewksbury, MA.

Thanks.
 

rleiman

Well-Known Member
Licensed User
Longtime User
If the distance between point A an B is not so great (e.g. < 100 km) you can assume that this part of the world is flat.
All you have to do is calculate the angle between A and B (relative to North).
Draw A and B on a piece of paper with X scale is 71 to 72 and Y scale is 42 to 43. This should give you insight how to calculate the angle.
To determine which way you are facing use the built-in magnetic sensors of your device or use GPS. In case of GPS you will have to move about 10 meters for the GPS to be able to determine your direction (bearing).
If both angles are the same then you are facing Tewksbury.

Hi Syd,

The location will be over 500 miles away. I don't think the user will want to move forward any amount. They would be turning left or right without moving forward or backwards.

I think I will try the GPS library with location.BearingTo() later to see what happens.

Are there any other libraries I can use?

Thanks.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Thanks for the replies. I will be using the phone library and the GPS library to do it.

My target coordinates to face are at latitude 29.907371 and longitude 31.184603 so I used this from the GPS library by adding the following code to the GPS library sample app.

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: GPS
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
#End Region
#BridgeLogger: true
Sub Process_Globals
    
End Sub

Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
    Private LabelQibla As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
    If Starter.GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then CallSubDelayed(Starter, "StartGPS")
    End If
End Sub
    

Sub Activity_Pause (UserClosed As Boolean)
    CallSubDelayed(Starter, "StopGPS")
End Sub

Public Sub GpsStatus (Satellites As List)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("Satellites:").Append(CRLF)
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite = Satellites.Get(i)
        sb.Append(CRLF).Append(Satellite.Prn).Append($" $1.2{Satellite.Snr}"$).Append(" ").Append(Satellite.UsedInFix)
        sb.Append(" ").Append($" $1.2{Satellite.Azimuth}"$).Append($" $1.2{Satellite.Elevation}"$)
    Next
    lblSatellites.Text = sb.ToString   
End Sub

Public Sub LocationChanged(Location1 As Location)
    Dim locQibla As Location
    locQibla.Initialize2("29.907371", "31.184603")
    
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text = $"Speed = $1.2{Location1.Speed} m/s "$
    LabelQibla.Text = "Qibla = " & Location1.BearingTo(locQibla)
End Sub

This above coding returned 58.6 degrees. I also checked a web site that calculates the same thing and it came up with 60 degrees. I imagine the GPS library was more accurate.

I got the phone bearing using this coding:

B4X:
#Region  Project Attributes
    #ApplicationLabel: Compass Heading
    #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 LabelCompassHeading As Label
    Private emad2 As PhoneOrientation
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
    emad2.StartListening("Emad2")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Emad2_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float)
    LabelCompassHeading.Text = NumberFormat(Azimuth,3,0)
End Sub

When my bearing was at 59 degrees. I achieved what I needed.

Thanks Erel for that GPS and phone library.

Thanks Syd for the link on calibrating the compass. I will read it to see if it can be done. I was actually going ask about that on another thread. :)
 
Upvote 0
Top