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

AndyMinsk

New Member
Licensed User
Longtime User
Hi. I have the same problem with

Lat:
Lon:
Speed:
Sattelites:
[five or six sattelites here]

I see more than three sattelites, but Lat/Lon/Speed is not shows. What I should do?

p.s HUAWEI U8650 Sonic
 

GaNdAlF89

Active Member
Licensed User
Longtime User
With Sub GPS_LocationChanged (Location1 As Location) I know when I receive coordinates from gps...There is any way to know when there is no reception of gps?
 

GaNdAlF89

Active Member
Licensed User
Longtime User
I mean an event, like LocationChanged, that execute when I call gps but it don't responding cause no reception.
 

konisek

Member
Licensed User
Longtime User
I receive the coordinates almost every second but the speed only sometimes, let's say every 4 seconds. The rest of time the speed textbox is empty. Moreover, the speed is not corresponding to a real value. I go by a car at 60 km/h and the speed shows around 15,5 km/h. What is wrong? (L)
 

Stulish

Active Member
Licensed User
Longtime User
questions, questions everywhere

Hi all, i have a few questions about the GPS functions in B4A.

1. will we ever be able to get the GPS NMEA sentences directly? (i take it the they are standard GPS receivers in handsets)

2. Is there a way to get the magnetic variation?

3. is the output in WGS84?

Thanks
 

Stulish

Active Member
Licensed User
Longtime User
Thanks Erel,

I need the data as a lot of my work is with NMEA serial devices not only the installation but i also come up with new ideas for the business. and being able to use android devices is the way forward (as i see it) :)

It would be useful to get the NMEA data as currently i have to get the location data and then format it into the NMEA sentences, if i could just output them it would be a lot faster.

Also more data is contained within the NMEA than is available through the GPS location library.

but having said that i am having some fun playing with it

Thanks

Stu

i will have a look at how the reflection library works to get the geomagnetic field data.
 

konisek

Member
Licensed User
Longtime User
Your example from 1st post shows Lat 32:50,23337 and Lon 35:16,17923.
I suppose, it is Israel, i.e. Lat 32:50,23337 (N) and Lon 35:16,17923 (E).
What does it show when I am in Rio de Janeiro, is it with negative value, i.e. Lat -22:54,89898 and Lon -43:12,02362?
Is there a simple way to show "N" and "E", or "S" and "W" rather than to distinguish it according to positive/negative values?
(L)
 

klaus

Expert
Licensed User
Longtime User
B4X:
Sub Lat_N_S(Lat As String) As String
    If Lat.CharAt(0) = "-" Then
      Return Lat.SubString(1) & "(S)"
    Else
      Return Lat & "(N)"
    End If
End Sub

Sub Lon_E_W(Lon As String) As String
    If Lon.CharAt(0) = "-" Then
      Return Lon.SubString(1) & "(W)"
    Else
      Return Lon & "(E)"
    End If
End Sub
B4X:
Lat = "32:50.23337"
Log(Lat_N_S(Lat)) ' 32:50.23337(N)

Lon = "-36:16.17923"
Log(Lon_E_W(Lon)) ' 36:16.17923(W)
Best regards.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Something like:
B4X:
Dim p1, p2 As String
If Location1.Latitude < 0 Then p1 = "S" else p1 = "N"
If Location1.Longitude < 0 Then p2 = "E" Else p2 = "W"
lblLat.Text = "Lat = " & p1 & Location1.ConvertToMinutes(Abs(Location1.Latitude))
lblLon.Text = "Lon = " & p2 & Location1.ConvertToMinutes(Abs(Location1.Longitude))


Edit: Double posted with Klaus...
 

konisek

Member
Licensed User
Longtime User
Yes, I know, I can do it this way. I meant if there was a function which does it. Apparantly, it is not. :)
 
Status
Not open for further replies.
Top