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: 5,501
Last edited:

Rusty

Well-Known Member
Licensed User
Longtime User
in the unfiltered logs I discovered:
[VERIFY] PACKAGE_VERIFIED:
PackageVerificationState{
verifier packages=com.android.vending(10034),
intent=Intent { act=android.intent.action.PACKAGE_NEEDS_VERIFICATION dat=file:///storage/emulated/0/Android/data/anywheresoftware.b4a.b4abridge/files/temp1.apk typ=application/vnd.android.package-archive flg=0x10000001 cmp=com.android.vending/com.google.android.vending.verifier.PackageVerificationReceiver VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} (has extras) }
others=95, false, false, false, false, false
}
[VERIFY] packageVerificationCompletion: completed id=95
[VERIFY] setVerifierResponse {verificationID=95, uid=10034, code=1
[VERIFY] broadcastPackageVerified(95, VERIFICATION_ALLOW, file:///storage/emulated/0/Android/data/anywheresoftware.b4a.b4abridge/files/temp1.apk)
copyApk
Killing 6454:com.amazon.mShop.android/u0a84 (adj 15): empty for 1805s
remove MCS_UNBIND and Posting MCS_UNBIND
Make sure sdcard is mounted.

The app was set to #CanInstallToExternalStorage: True and my phone does not have an SD card installed.
I changed it to #CanInstallToExternalStorage: False and it now installs.
Thanks Erel, I should have looked at the un-filtered logs before.
Regards,
Rusty
 

Peekay

Active Member
Licensed User
Longtime User
Erel,
In the above code you refer to a routine rp in module Starter, but I do not find that in your example code.
 

kris A

Member
Licensed User
Longtime User
How accurate location GPS it show me on map 30m from where I was
is it possible to take this down to 2m - 10m?

My device : Xiaomi Redmi Note 2
Android version : 5

Thanks
 

Husain Ragib

Member
Licensed User
Erel, I am abeginner in b4a.
I downloaded the GPS example and tried to see the output via. B4A bridge connected with my android phone.
But I could get any information against the labels.
GPS is enabled in my phone. Can you please help me
 

svein berg

Member
Licensed User
Hi.
I'm new to B4A, but so far I like it.
Have a problem with the GPS.

I would like the "GPS1_LocationChanged (Location1 As Location)" to fire every 2 seconds or if Location have changed more than one meter.
So I put;
gps1.Start(2000,1 )

But then the "changed event" keep firing like mad without the location changing at all. (Phone is still on my table)

In the "Activity_Create" - FirstTime I have;
If gps1.IsInitialized Then gps1.Stop
as I dont want it to fire before I have initiated a logon procedure, but its firing..

I'm using B4A v 6.0 and have a Samsung with Android 6.0

Any help??
 

svein berg

Member
Licensed User
Thanks Erel..
GPS Error is not part of this as I display the Lat,Lon in each LocationChanged, and the error is much less than 1 meter.

Isnt it possible to stop the Activity from listening to the GPS?
 

gregmatthews

Member
Licensed User
All the examples get a lat, lon pair in the locationchanged event. Does this mean the event needs to fire at least once to get a location ? What happens if you start the app and are perfectly still? Can you still get a location?

Thanks
 

gregmatthews

Member
Licensed User
Hi Erel,
Thanks - my issue must be somewhere else then!
To clarify:
The locationChanged event will fire either after a certain number of milliseconds OR after a change in position, the thresholds for each set via the GPS.Start(x,y) function. So if that is GPS.Start(0,0) then it will rapid fire. If GPS.Start(5000,50) if will fire every 5s or if the position changes by 50m, but not more frequently than that?

Greg
 
Status
Not open for further replies.
Top