Android Tutorial GPS / GNSS tutorial

Status
Not open for further replies.
The GNSS library replaces the old GPS library. The GNSS library is available here: https://www.b4x.com/android/forum/threads/gnss-library-an-updated-gps-library.109110/#content


1741875843450.png


Using it is quite simple:
1. Checking whether location services are available. If not take the user to the settings.
2. Request permission.

If the purpose is to get the user location then the FusedLocationProvider might be a better option: https://www.b4x.com/android/forum/threads/fusedlocationprovider-resolution-dialog.111652/#content
 

Attachments

  • GPSExample.zip
    15 KB · Views: 451
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