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:

systems1

Member
Licensed User
Longtime User
Help for to get GPS coordinates

Hi Erel,

I tried the code you attached.

When I run the program in my Emulator,

I get a message that "GPS device enabled=true".

But i did't get GPS cordinates as out put.

I see we have two more events

B4X:
Sub GPS_UserEnabled (Enabled As Boolean)
//statements
End Sub 
And 

Sub GPS_LocationChanged (Location1 As Location)
//statements
End Sub

But I understand there is no event executed for GPS_UserEnabled and GPS_LocationChanged.
I printed a message box alert inside there. But I didn't get any alert message from that section.

My GPS Lib version is 1.00 and Core Version is 1.91

Can you help me ; I want to get GPS Latitude and Longitude of current location.

Regards,
:sign0085:
 
Last edited:

zekigultekin

Member
Licensed User
Longtime User
GPS library can not support motorola device

Dear Erel,
I use motorola xoom2 tablet. I test your GPS sample on this. But, I cannot get location information from motorola GPS.

Can you help me for solve my problem.

Best regards,

Zeki
 

GaNdAlF89

Active Member
Licensed User
Longtime User
why I received the date of tomorrow?? O.O
today is 08-03-2012 and I received 08-04-2012!!
the time is correct...
 
Status
Not open for further replies.
Top