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

klaus

Expert
Licensed User
Longtime User
Location1.Bearing is the current direction (angle to the North) in degrees according to the previous location.
Location1.BearingTo(Location2) is the orientation (angle to the North) in degrees between the two locations.
Location1.DirectionTo(Location2) is the distance between the two locations in meters.
 

GMan

Well-Known Member
Licensed User
Longtime User
I am using the GPS features since my start with b4a and it works always fine.

Now, in a new app, my actual position is not correct, it shows IMHO the Provider location, even if i disable the GPS feature "Network provider or WLan position"

The GPS example also shows a wrong place (apporx. 30 km away from me) . i just copied the code from this TuT to my new app.
as it not works, i work through the Tutorial again, but i found nothing strange or so...its quite simple to use the GPS.

P.S.: the GPS example V 2.1 works correct !

Has anyone a suggestion ?
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Yes.
but when i start an navigation to any point the starting point in GoogleMaps is the correct one
 

GMan

Well-Known Member
Licensed User
Longtime User
If it returns wrong results then there is a problem on your device.
But what can that be ?
i am using the pure normal GPS example...no settings(options anywhere ;-) for a "offset" or something like that
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Is there a way to smooth out the GPS numbers.

I am running the GPS example and there numbers keep changing while I am not moving 42.xx 45.xx 39.xx (these are distance to numbers) Now I know there is a +- 1 meter variance.

But is there an algorithm that smooth this out? I tried storing X (5 / 10) samples and then averaging them but that did not help much.
 

GMan

Well-Known Member
Licensed User
Longtime User
On my problem there is no change.
One runs, the other not with the correct positions.

Are there several choices to initialize the GPS ?

As i can see, in the V2.1 sample there is no other init as in my own app - but it works
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Try to find the median of the last 10 samples. It should be quite smooth.


I did a Median 5 and that helped a lot - will see if 10 is any better.
I also changed my sampling from Start(0, 0) to Start(500, .5) <-- which I believe should give every half meter changes?

With Start(0, 0) there is so much bouncing without moving (tablet on desk) why would the numbers fluctuate like this? Is this the government misleading data?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
which I believe should give every half meter changes?
Yes.
The first parameter is the minimum time (milliseconds) between events and the second parameter is the minimum distance (meters).

With Start(0, 0) there is so much bouncing without moving (tablet on desk) why would the numbers fluctuate like this? Is this the government misleading data?
Hard to say. The GPS library just passes the data from the GPS driver.
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am using RandomAccessFile to ReadObject and WriteObject and now have included a Location object in my Type and this field is either not being written out or read in. When I read the file after writing it I get a Null value for the Location type

Could you verify that ReadObject and WriteObject are handling GPS Location objects correctly?

I have lots of other different types of objects (Strings, Lists, Booleans, etc) and they are all written and read properly

B4X:
	Type  TGPSDisplay(DisplayAt As String, Coordinates As Location)

The type above is used as entries to a list

Thanks BobVal
 

laviniut

Active Member
Licensed User
Longtime User
I searched on the internet and i read about coordinates. so in gps tutorial is gps coordinates. i need to convert it to wgs84 (decimal degrees).
 

Kevin L. Johnson

Member
Licensed User
Longtime User
I've recently upgraded my dev platform to KitKat running on my Samsung Galaxy S3 and now I must turn off location services before running my app again from the IDE (otherwise I get an empty screen except for labels) The code I'm using is the GPS tutorial code found at the top of this forum. I don't know why this is and am wondering if anyone else is experiencing this.

Thanks
Kevin

UPDATE: It actually will eventually recover using the GPS Tutorial code but takes a really long time like perhaps 10 plus minutes? Is there any reset code sequence that I could use to force it to reset / recover faster?
 
Last edited:
Status
Not open for further replies.
Top