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

achtrade

Active Member
Licensed User
Longtime User
It is not strange. Google Maps uses several sources to find the location. It can take the GPS several minutes to find the location.

It is not related to wifi.

I just found out that the GPS_LocationChanged sub isn't fired when the app start. Is there any way to force it to fire when the app start ?
 

incendio

Well-Known Member
Licensed User
Longtime User
Got these error when running the example

B4A version 4.30
Parsing code. 0.00
Compiling code. 0.02

ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code. Error
Value cannot be null.
Parameter name: input
Error File: 1.bal

Go to the designer script page to debug this error.

EDITED :
It seem that the layout made in the old version.

Saved again with ver 4.3 fix the error.
 
Last edited:

Glitchs

Member
Licensed User
Longtime User
Hello.

I cant see the source code either the libraries, then I put muy question: It's posible to get also the quality of measure GPS like the accuracy in meters o the DOP?
I want to crate an app using gps but is very important if the measured lat and long are good fixed.
Best regards.
 

Glitchs

Member
Licensed User
Longtime User
Thanks. A very good news. The Begginers Manual dont explain if this feature es available, and I did not find where to read about it.
Best Regards
 

nicodh

Member
Licensed User
Longtime User
Hello Erel,
I'm trying to use the GPS library. I needd to retrieve the altitude, my code works, but the altitude is wrong.
The notification icon from android tells me 238 meters with an error of around 13 meters (wich is the correct altitude) but the gps tutorial or my app said i'm at 278 meters with an accuracy of 38 meters.
Am i missing something?
Why there is a difference between the notification from android and the b4a library?
Thanks.

BTW: I'm using android 4.4.2.
 
Last edited:

nicodh

Member
Licensed User
Longtime User
Sorry for that erel, it wasn't intended.

Thanks for the link i'm reading it right now.
 

Glitchs

Member
Licensed User
Longtime User
Hello Erel,
I'm trying to use the GPS library. I needd to retrieve the altitude, my code works, but the altitude is wrong.
The notification icon from android tells me 238 meters with an error of around 13 meters (wich is the correct altitude) but the gps tutorial or my app said i'm at 278 meters with an accuracy of 38 meters.
Am i missing something?
Why there is a difference between the notification from android and the b4a library?
Thanks.

BTW: I'm using android 4.4.2.


Hello,

Your error could be the Geoid Ondulation, that is the diference of altitude between the Orthometric altitude and the elipsoidal altitude. All the GPS send the elipsoidal altitude. Then you have to know the Geoid Ondulation in order to correct the real altitude. For example here in spain the G.O. is about 45 meters.

Best regards.
 

吳界明

Member
Licensed User
Longtime User
Could you give me an example, about when GPS position has been changed, that can Real-time display on the map updated location and marked
I want use the Webview component and not use google map API key
 

chrjak

Active Member
Licensed User
Longtime User
Hey,

I'm having a problem with GPS. It's in a service (started in foreground) and everything works. The Service normally doesn't get killed.

But some days ago i noticed a big problem.
My Phone had about 15% battery and when i locked my phone the service got paused. Not killed and it resumed immediately after unlocking my screen but that mustn't happen. It's for recording the location and when my screen was locked there were no points saved. How to solve that? Or better. Why does this happen?

Thanks for your reply
 

SCIS

Active Member
Licensed User
Longtime User
Hi, I made a whole new project and used the exact same code as you. I also made the same layout with the same name but there's no txt showing up in the labels. The GPS icon is blinking tho so that should be working.
What should I do?

Thanks in advance!
 

SCIS

Active Member
Licensed User
Longtime User
Satellites are working now but everything shows up as "false" instead of true. Latitude and longitude still aren't working.
 

Danbicky

Member
Licensed User
Longtime User
Erel,

This is great, lets say I want a 1 mile radius from my home position, and to raise some form of alert if my phone goes outside of this, how is this achieved? Like a Geo Zone!

Big thanks

Dans
 
Status
Not open for further replies.
Top