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

Stulish

Active Member
Licensed User
Longtime User
1. The underlying LocationManager object does expose the NMEA sentences. It may be added in the future. Why do you need the raw data?

2. You can use Reflection to get the values with this class: GeomagneticField | Android Developers
I can help you with this.

3. It should be WGS84.

Erel,

do you have any examples of using the reflection library to get to the NMEA data??

Thanks

Stu
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
GF(33, 33, 100, DateTime.Now)

Sub GF(lat As Float, lon As Float, altitude As Float, time As Long)
   Dim r As Reflector
   r.Target = r.CreateObject2("android.hardware.GeomagneticField", _
      Array As Object(lat, lon, altitude, time), _
      Array As String("java.lang.float", "java.lang.float", "java.lang.float", "java.lang.long"))
   Dim declination As Float = r.RunMethod("getDeclination")
   Dim fieldStrength As Float = r.RunMethod("getFieldStrength")
   Dim x As Float = r.RunMethod("getX")
End Sub
It should be simple to call the other methods.
 

Stulish

Active Member
Licensed User
Longtime User
Thanks Erel,

is there an easy way to implement

GpsStatus.NmeaListener

This contains a method containing the NMEA String this is the data i am looking for and currently converting location information using the GPS library into, this would mean i do not need to format the data and could just pass it through.

Thanks

Stu
 
Last edited:

Stulish

Active Member
Licensed User
Longtime User
Thanks again Erel,

I wasn't sure if a reflector could be used to get this string. so i will look for the update in the future.

Stu
 

Stulish

Active Member
Licensed User
Longtime User
That is great Erel, i din't expect it to be done so quickly and the sentences are just what i was after.

:sign0098: i think Great Job (but the Smilies don't have that on the notice board so i settled for Good Job :)
 

micp

Member
Licensed User
Longtime User
No position found using the demo.

Hi, i am more then a begginner into programming and i tryed to run the exemple source code to learn how it's working but no position is found using it and all satelites are return a false value. What am i doing wrong?
 

konisek

Member
Licensed User
Longtime User
Sometimes, the application pops up with error message:

main_gps_locationchanged (B4A line: 143)
Label3.Text = NumberFormat(Location1.Altitude,1,0) 'nadm. vyska
java.lang.RuntimeException: Object should first be initialized (Label).


Sometimes it also occurs with lblLat and lblLon and then
java.lang.RuntimeException: Object should first be initialized (Location).

As it happens rarely only, I do not know what is wrong with the code. Any advice, pls?
If Location1.Latitude < 0 Then
p1 = "S"
Else
p1 = "N"
End If

If Location1.Longitude < 0 Then
p2 = "W"
Else
p2 = "E"
End If

'dvoumistne cislo
stupneLat = Floor(Location1.Latitude)
If Floor(Location1.Latitude) < 10 Then stupneLat = "0" & stupneLat
minutyLat = (Location1.Latitude - Floor(Location1.Latitude))*60
If minutyLat < 10 Then minutyLat = "0" & minutyLat

stupneLon = Floor(Location1.Longitude)
If Floor(Location1.Longitude) < 10 Then stupneLon = "0" & stupneLon
minutyLon = (Location1.Longitude - Floor(Location1.Longitude))*60
If minutyLon < 10 Then minutyLon = "0" & minutyLon

'skutecne koordinaty (pro srovnani)
'Lat = Location1.ConvertToMinutes(Location1.Latitude)
'Lon = Location1.ConvertToMinutes(Location1.Longitude)

lblLat.Text = p1 & ": " & (stupneLat & "°" & minutyLat).substring2(0,11) & "'"
lblLon.Text = p2 & ": " & (stupneLon & "°" & minutyLon).substring2(0,11) & "'"
Label3.Text = NumberFormat(Location1.Altitude,1,0) 'nadm. vyska
 

JTmartins

Active Member
Licensed User
Longtime User
A dumb question ??

Hi Guys,

This may seem a dumb question, bit if GPS can not be enabled programmaticly, how comes that I have a widget to turn on and of the GPS & Wi-fi, etc in my phone, and works ok ?

Thanks.
 

RichardHirst

Member
Licensed User
Longtime User
Input GPS string to GPS Library

Hi Erel,

Is or would it be possible to read a GPS string from a file or external serial port and pass this to the GPS Library to parse.

I have seen someone has a GPS parse code base but was hoping to be able to use the standard library.

Great work, just started to play with the new Google Maps library too :).. oh can you rotate a custom marker ?


Thanks

Richard.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is or would it be possible to read a GPS string from a file or external serial port and pass this to the GPS Library to parse.
No, it is not possible. The parsing is done in a lower level than this library.

oh can you rotate a custom marker ?
Currently it is not possible. You can use a different bitmap, though the actual point will still be the bottom - center of this bitmap.
 

bushfire

New Member
Licensed User
Longtime User
NMEA Sentences

Hi Erel,

Are the NMEA sentences returned dependent upon the type of android device, or set by b4a?

In particular, I'm looking for two NMEA sentences that are prefixed by GPGGA and GPVTG. I'm using a Samsung Galaxy S for my app and it seems to be only returning PGLOR, GPGSV, and GPGSA.

Cheers
 

tsteward

Well-Known Member
Licensed User
Longtime User
Using the GPS library I get the following returned:
Lat = -36:3.20404
Lon = 146:59.30533

Now still sitting in the same chair, in Evernote I create a note that records my current GPS as
-36.204040
146.305330

My app creates notes in evernote and I need the same format as Evernotes.
I have read this thread, possibly missed the info I need. But is there a way to convert from one the top pair of Lat Lon to the evernote type pair.
 
Status
Not open for further replies.
Top