Android Tutorial GPS tutorial

Status
Not open for further replies.
This example shows how to work with the GPS library.

upload_2016-6-23_9-56-49.png


The GPS object is declared in the Starter service. It is easier to put the GPS in a service and not in an Activity as the services life cycle is simpler.

When the program starts we check whether the location features are enabled:
B4X:
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
If not then we open the relevant settings screen.

The next step is to check for the ACCESS_FINE_LOCATION permission (runtime permissions tutorial).
If we have permission then we call the StartGPS sub from the Starter service.

Now all that is left is to delegate the LocationChanged event from the service to the activity and show the information.
B4X:
'Starter service
Sub GPS_LocationChanged (Location1 As Location)
   CallSub2(Main, "LocationChanged", Location1)
End Sub

'Main activity
Public Sub LocationChanged(Location1 As Location)
   lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
   lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
   lblSpeed.Text = $"Speed = $1.2{Location1.Speed} m/s "$
End Sub

Due to Google policy change you also need to add this line to the manifest editor:
B4X:
AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)
This will prevent the app from being installed on devices without GPS hardware.

The example is attached.

Modified example with NMEA listener: https://www.b4x.com/android/forum/t...-not-firing-on-android-10.112140/#post-699351
 

Attachments

  • GPS.zip
    8.1 KB · Views: 7,233
Last edited:

schimanski

Well-Known Member
Licensed User
Longtime User
Result of bearingto is negativ

Hello!

I have calculate the bearing from my own position to the target-position with

B4X:
Kurs=GPS1.BearingTo(Target)

The result of Kurs is negativ and not equal with the real bearing. :confused::confused:
 

schimanski

Well-Known Member
Licensed User
Longtime User
Hello Erel!

Is it possible, that the bearing is not shown in 0-360°? When it is shown from 0-180 and -180 to 0°, I think it is correct...:sign0013:
 

peacemaker

Expert
Licensed User
Longtime User
So, isn't possible programmatical on\off (connect\disconnect) of Android's GPS subsystem (like in WM 5.x-6.x)? Without manual user's choice.
I mean periodical GPS auto-connect by a software only.
 

agraham

Expert
Licensed User
Longtime User
So, isn't possible programmatical on\off (connect\disconnect) of Android's GPS subsystem (like in WM 5.x-6.x)? Without manual user's choice.
No, it's not possible. As Erel stated above.
Due to privacy concerns the Android OS doesn't allow you to turn the GPS on programatically.
 

peacemaker

Expert
Licensed User
Longtime User
Ha, so this is enough. Just an extra manual permission from a user, i see.
Thanks, Erel.
Now just interesting about background running application and switching the screen off, again for battery economy.
 
Last edited:

ZJP

Active Member
Licensed User
Longtime User
Hi,

Good work ;) but what about the NMEA ? ( hxxp://developer.android.com/reference/android/location/GpsStatus.NmeaListener.html )
How to get those frames? :confused:

Thx
 
Last edited:

ashrafidkaidek

Member
Licensed User
Longtime User
Why when I try this code no message shows up when pressing the home button?

B4X:
Sub Activity_Pause (UserClosed As Boolean)

   Dim Results4 As Int
   Results4 = Msgbox2("Just a msg", "Try","OK", "", "", LoadBitmap (File.DirAssets,"Signal.png"))
   If Results4 = DialogResponse.POSITIVE Then
   End If
   
   GPS1.Stop
   Awake.ReleaseKeepAlive

End Sub

Thanks
 

ashrafidkaidek

Member
Licensed User
Longtime User
Thanks Erel, I have noticed one other thing that dose not make since to me:
I have created a small GPS app following your example above; (just to understand the process correctly). What I have tried to do is to make the app shows all the information to the user (Lat, Lng. …) and do some other tasks in the background (track the user movement).
What I have noticed that while the app is running everything works fine until the user gets a phone call which forces my app to pause, and force the GPS to turn off, therefore I changed the code as shown below:

B4X:
Sub Activity_Pause (UserClosed As Boolean)

End Sub

Note: I have added an if statement to the “Activity_Resume” sub to insure that I’m not calling GPS1.Start while the GPS is already on)

Now when the app being pushed to the background because of a phone call (or maybe the user decided to play music or surf the Internet) the GPS will stay on, but the application somehow will be paused (the tracking function is not recording anything while the application is in the background)

Is this issue due to Android hardware limitation? If yes then how come you can play music and surf the Internet at the same time?

Thank you all
 
Last edited:

ZJP

Active Member
Licensed User
Longtime User
That means it is impossible to have a background processing? :confused:

JP
 

ashrafidkaidek

Member
Licensed User
Longtime User
Erel,

If I understand this correctly, this is a Basic4Android limitation, it is not Android operating system limitation, is that right?

And when you say in the future dose that means the next Basic4Android version?

Regards
:sign0148:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If I understand this correctly, this is a Basic4Android limitation, it is not Android operating system limitation, is that right?
No, that is not correct. This is Android OS limitation (or actually design).

And when you say in the future dose that means the next Basic4Android version?
It is planned for the next major update.
 
Status
Not open for further replies.
Top