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

GaNdAlF89

Active Member
Licensed User
Longtime User
How I can set my time zone?

In my code, I've modified only with
B4X:
DateTime.DateFormat = "dd/MM/yyyy"
today=DateTime.Date(GPS_Timestamp)
...
 
Last edited:

mjtaryan

Active Member
Licensed User
Longtime User
I think this is quite useful. However, for my current purposes I'm wondering if there is a way to read the current device location from the system? If so, how? Thanks.
 

GaNdAlF89

Active Member
Licensed User
Longtime User
B4X:
Sub Service_Create
    DateTime.SetTimeZone(1)
End Sub


Sub MyGPS_LocationChanged (MyLocation As Location)

   TimestampGPS = MyLocation.Time
   
   DateGPS= DateTime.Date(TimestampGPS)
   TimeGPS= DateTime.Time(TimestampGPS)
End Sub

Timezone of device is set to +1. I receive the date of tomorrow, and the time of an hour ago. Please help me!
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
It seems like a problem in your device.

I tested the following code on multiple devices and it works as expected:
B4X:
Sub Process_Globals
   Dim g As GPS
End Sub

Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      g.Initialize("g")
      g.Start(0, 0)
   End If
End Sub
Sub G_LocationChanged (Location1 As Location)
   ToastMessageShow(DateTime.Date(Location1.Time), True)
End Sub
 

phast

Member
Licensed User
Longtime User
I am very new to this, so sorry if this is completely wrong. I am trying to find the distance between a set lat and lon against where I am currently located. My current code is:

B4X:
Sub GPS_LocationChanged (Location1 As Location)
   lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
   lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
   Location2.Latitude="40.4406"
   Location2.Longitude="79.9961"
   lblDist.Text = Location1.DistanceTo(Location2)
   'lblSpeed.Text = "Speed = " & Location1.Speed
End Sub

I have dim Location2 as Location

When I start the app it says "invalid double"




EDIT::

Sorry, I solved my own issue by removing the "" which I now assume makes the value a string?
 
Last edited:

michelg

Member
Licensed User
Longtime User
You can't enable or disable the GPS device. However if it is enabled it will turn on when it needed and off when it is not needed (to save battery).

Hi,

I was trying to run the GPS app in the emulator. when I run the app,
on the emulator's screen this is what I see:
Lat:
Lon:
Speed:
and a quick message saying
GPS device enable=True

However I do not see any location information.

what should I do to see the location information?
 

GaNdAlF89

Active Member
Licensed User
Longtime User
@Erel

I have reset my device, I set the timezone in settings to +1 (I'm italian).

In Main Activity:
B4X:
Sub Activity_Create
    DateTime.SetTimeZone(1)
    DateTime.DateFormat = "dd/MM/yyyy" 'I prefer this format
End Sub

In GPS_Service
B4X:
Sub MyGPS_LocationChanged (MyLocation As Location)
   GlobalModule.Latitudine = MyLocation.Latitude
   GlobalModule.Longitudine = MyLocation.Longitude   
   GlobalModule.Velocita = MyLocation.Speed*3.6 'I convert to km/h
   GlobalModule.TimestampGPS = MyLocation.Time

   'now I convert timestampGPS in date and time
   GlobalModule.DateGPS = DateTime.Date(GlobalModule.TimestampGPS)
   GlobalModule.TimeGPS = DateTime.Time(GlobalModule.TimestampGPS)
End Sub

I receive the date of tomorrow, and the time of an hour ago...I don't understand what's the problem!
Please help me
 
Last edited:

GaNdAlF89

Active Member
Licensed User
Longtime User
I tried to convert the gps timestamp with an online converter, and I obtain the same result of my app...why??
 

michelg

Member
Licensed User
Longtime User
The GPS is an important feature of many Android devices.
Fortunately it is pretty easy to work with it.

Hi,

when I run the GPS program in the emulator this is what I see:


Lat:
Lon:
Speed:
Satellites:

I do not see any information:

I used b4a bridge to run it in my phone and the result is the same.

Can you please give me an idea of what could be wrong.

Thank you,
 
Status
Not open for further replies.
Top