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:

Beja

Expert
Licensed User
Longtime User
Hi Erel,

I am just wondering, what's the use of the satellite information? why one should know about them when he already has the lat/lon info?
Thanks
 

IslamQabel

Active Member
Licensed User
Longtime User
No thing to wonder, as i understood May Erel want to show us all available options for members of GPS objects.....false or true in satellite indicating to find a fix or not.....if false is displayed, you got nothing about your location but if it is true ....your co-ordinates will be shown.....I am was complaining that i did not receive my location until many satellites show true state and then my co-ordinates are displayed..............in addition to Signal to noise ratio indicate to the received signal strength....if you have small ratio, your reception is not good and you got false state....
I would like to thank Erel for his great help and support
 

Beja

Expert
Licensed User
Longtime User
Thanks,
That helps and I now know some uses of the sat info.
 

Beja

Expert
Licensed User
Longtime User
edit:
sorry wrong answer because the question is about a file.
 
Last edited:

IanMc

Well-Known Member
Licensed User
Longtime User
Is there a simple method to detect if a tablet has GPS ?

Ah, I think I found Erels 'has feature' sub which should do the trick.

B4X:
Sub Activity_Create(FirstTime AsBoolean)
   Log(HasFeature("android.hardware.telephony"))
End Sub

Sub HasFeature(feature As String) As Boolean
  Dim r As Reflector
  r.Target = r.GetContext
  r.Target = r.RunMethod("getPackageManager")
  Return r.RunMethod2("hasSystemFeature", feature, "java.lang.String")
End Sub

I'll do some experiments with this... should be "android.hardware.gps" methinks?
 
Last edited:

IslamQabel

Active Member
Licensed User
Longtime User
Thanks Erel...works good.....
When i use the following
B4X:
Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text = "Speed = " & Location1.Speed
    Writer.WriteLine(lblLat.Text & "    " & lblLon.Text & " "lblSpeed.Text)
    Writer.Flush
End Sub
a file stored in my mobile and all info are displayed successfully
But I would like to ask you about....When i forgot to write "Writer.Flush".....No data were stored in the file but when i added this line, all data are stored.....
 

IslamQabel

Active Member
Licensed User
Longtime User
Another question which is better storing in txt file or SQLlite data base?.....can we store GPS data to excel sheet?

Thanks
 

IslamQabel

Active Member
Licensed User
Longtime User
Erel....After i succeeded to store data on a txt file and using textwriter.writeline, i opened the file by mobile and data were written line by line and no problem but when i opened the txt file by my PC , all the data were written in a very long line....
I read in help file about End of Line....., you said that if you want to create a file for windows , you should add CR and LF by yourself.......so how to save the data in a txt file line by line

Thanks
 

Beja

Expert
Licensed User
Longtime User
B4X:
Writer.WriteLine(lblLat.Text & "    " & lblLon.Text & " "lblSpeed.Text) & CRLF

DIDN'T TEST THOUGH
 

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi, I want that user can enable gps device if it is disabled:
B4X:
If MyGPS.GPSEnabled = False Then

    Dim DoAction As Intent
    DoAction.Initialize("android.settings.LOCATION_SOURCE_SETTINGS","")
   
    StartActivity(DoAction)

End If

How can I resume my app automatically when the gps device is enabled by user? Thanks!
 
Status
Not open for further replies.
Top