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:

Kevin L. Johnson

Member
Licensed User
Longtime User
It sounds like a problem on your device. You cannot reset the GPS. There is no API for that. You can try to call GPS.Stop and then GPS.Start. Though it will probably not help in this case.


Ok, Thanks Erel.

I did notice that when I upgraded to KitKat, my "GPS" Icon in the pull-down "Settings" section changed to a "Location" icon. If I turn off "Location" Services prior to re-running the code and let the tutorial code ask the user to enable GPS (via the "Location" services Icon) then everything runs as before. I just don't like the idea of needing to turn off "Location" services every time before running a GPS dependent app. I would like to know if anyone else might be experiencing this.

Thanks
 

GMan

Well-Known Member
Licensed User
Longtime User
I have several App using GPS (also as BT etc.)
SOME Apps Stops the GPS when finishing (GPS.Stop), others not: if a Apps is used i.e. once a day, the Service will be stopped, if the App is used several time the GPS keep running in the background.
At all, the Service is only used if an App needs it.

So you may set it as a Setup- or Option-parameter to let the user decide
 

Kevin L. Johnson

Member
Licensed User
Longtime User
You don't need to disable Location in order to use the GPS. Maybe the location settings are set to low accuracy mode.

In fact, I have been using the 'GPS only' Mode (which is not the 'High Accuracy' Mode and therefore presumably considered 'Low accuracy'). Additionally, the symptom is as follows ... if I don't manually disable "Location" AND let the app prompt the user to enable "GPS" (which is done by enabling "Location") THEN the app takes takes an unusually long time (10 to 15 min) before the GPS data begins streaming.

UPDATE: I've just run my location services in High Accuracy Mode two times in a row and communication with the GPS was picked up almost immediately. I need to run some more tests for now.
 

AndyW999

Member
Licensed User
Longtime User
Hi

I am running the GPS as a service and starting it off with a timer :-

Sub tmrGPS_Tick
GPSService.Start(0, 0) 'Listen to GPS with no filters.
ServiceCounter = 2

End Sub


I then have :-

Sub GPSService_LocationChanged(Location1 As Location)

If ServiceLocation.Accuracy <=16 Then
do stuff...
GPSService.Stop
end if
End Sub


But it does not stop!

Any ideas?
 

vincentehsu

Member
Licensed User
Longtime User
Dear Sir:
I've download the sourcecode,but when i built up the project and run as acturally mobile device,I can't get the lat and lon data?
 

stefanoxjx

Active Member
Licensed User
Longtime User
Hi guys, I've a strange problem :(
I'm testing the sample app of this post, but the app returns me wrong coordinates.
It returns me for latitude 45:37,76xxxx and longitude 12:1,68xxxx instead real coordinate 45:62,xxxx / 12:0,2xxxx.
I've tested this app in two different device and have same problem :(
Anyone have some ideas?
Thanks.
Best regards.

Stefano
 

achtrade

Active Member
Licensed User
Longtime User
Today, the GPS stopped working.

I have an app with GPS and everything was working fine til now, I'm using galaxy s 3 and galaxy tab 3 7.0 without problem and suddenly today they 2 are not working. I'm not getting any error, just lat and long with value 0.

I tried google map and it's working fine, only my app can't find my location. Also, I installed this example and it doesn't get any lat/long, just this msg "Satellites: 16.20 False 159.30xxxxxxxxxxxxxxxxx.5

What's going on ?
 

achtrade

Active Member
Licensed User
Longtime User
Google Maps uses other sources to find the location if GPS is not available.

Are you testing it outdoors?

I'm only using wifi, those devices don't have cellphone service. Is possible that GPS does not work well only with wifi ?. The strange thing is that Google Map App is working fine only with wifi

Note: After left open my app for several minutes, like 5 or 7 minutes, finally the GPS found my location. What can be happening ?
 
Last edited:
Status
Not open for further replies.
Top