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

agraham

Expert
Licensed User
Longtime User
I assume the original readins are degrees:minutes so try this.
B4X:
Dim lon, degmin() As String
Dim evernote As Double
lon = "146:59.30533"
degmin = Regex.Split(":", lon)
If degmin(0) < 1 Then degmin(1) = -degmin(1)
evernote = degmin(0) + degmin(1)/60   
Msgbox(degmin(0) & CRLF & degmin(1),evernote)
 
Last edited:

gregmartin64

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.

In the Sub GPS_LocationChanged (Location1 As Location) you can get it like this

B4X:
dim lat as string
dim lng as string

lng=NumberFormat(Location1.Longitude,0,10)
lat=NumberFormat(Location1.Latitude,0,10)

Set the decimal places to how accurate you want the position ( I put it to 10 decimal places. )
 

tsteward

Well-Known Member
Licensed User
Longtime User
Thats sweet guys.

Thank you :sign0060:
 

jimmyjace

Member
Licensed User
Longtime User
GPS - Latitude Ref and Longitude Ref

Hi Erel,

Great tutorial on using GPS. I'm trying to get the Lat and Long Ref values which contain N,E,S,W but don't seem to be able to find them.

Any ideas where i get the settings?

Kind Regards,

Jason
 

LittleBritaly

Member
Licensed User
Longtime User
Can someone advise on how can I detect when the location has changed a set distance (10 metres for example).

Basically I want the phone to send me an sms/phone me if the location changes and periodically sms me it's location.

Thanks
 

nitinb4a

Member
Licensed User
Longtime User
GPS GeolocationDemo & using WebViewExtras

Hi Erel,

I am running the GeolocationDemo and the sample b4a program "WebViewExtrasDemo" is giving below error while compiling the program.

Parsing code. Error
Error parsing program.
Error description: Unknown type: webviewextras
Are you missing a library reference?
Occurred on line: 17
Dim MyWebViewExtras As WebViewExtras

Pls. tell me which library reference need to be add to run this program.
:sign0085:
 

nitinb4a

Member
Licensed User
Longtime User
Hi Erel,

Thanks Now I have added WebViewExtras library reference.

I am trying to run sample NinePatchExample and it is giving below error msg.

Parsing code. Error
Error parsing program.
Error description: Unknown type: reflector
Are you missing a library reference?
Occurred on line: 470
Dim r As Reflector


Pls. suggest the library reference for reflector

:sign0085:
 

nitinb4a

Member
Licensed User
Longtime User
GPS tutorials and Gps sample program issue

Hi Erel,

I am trying to run a sample b4a program "gps.zip " in "GPS tutorial" but it is not showing any output

Lat =?
Log =?
Speed =?
Satellite=?

I need to develop a android apps for my organization which will getGSMLocation or getGPSPosition of user cell phone and plot the Longitude & Latitudes on Map to track the visits of Sales executive like Google Vehicle tracker utilities.

Kindly guide me to develop the same and suggest related sample programs and tutorials and B4a documentations

Thanks :sign0085:
 

nitinb4a

Member
Licensed User
Longtime User
GPS tutorials and Gps sample program issue

Hi Erel,

I am running the GPS program Indoor. The GPS is enabled. The satellite may not be found.

When I searched various sample B4A programs for GPS. I found most of the sample codes in zip format with different dll files (Serial2.dll,GPS.dll) and visual basic modules for windows mobile i.e. "Gps4PPC" Since I am working for android mobile and purchased a license B4A software and I want to develop a apps to implement following tasks for our organization.

[ I ] Location Tracker.
a) Getposition () of user cell phone (Imei Number,longitude & latitude)
b) SetPosition () of user cell phone (Imei number,longitude & latitude)
c) FindDistance() or calculate the distance between two GPS locations.

I need more information and sample B4A (GPS/WiFI/Trangulation) codes to get user/my android mobile cell phone location data like (Device ID, Longitude,Latitude) and I want to save it at one online central Ms Sql database server and pass them to google ("mapnik") map so that it will show the cell locations as a pin points on google map like Google Vehicle tracking system.

Pls. advise me for the same.:sign0085:

Best Regards
Nitin
 

netsistemas

Active Member
Licensed User
Longtime User
Error In Altitude

The GPS return a bad altitude. This is not a error or bug.
But,
i see a application that return a real altitude, using some as 'with wifi detect'.

Are there some thecnique to get the correct altitude? (or more correct).
--------
El GPS devuelve una altitud erronea. Esto no es un error o fallo.
Pero,
He visto una aplicacion que devuelte la altura real, usando algo como 'detección por wifi'.
¿Hay alguna técnica para objet al altura correcta o al menos, mas correcta?
 

metheoneandonly

Member
Licensed User
Longtime User
Hello everyone!

I have tested this example on my HTC WildFire S, but it doesn't work as expected. The problem is in speed measurement, it shows me only the fraction of actual speed. For example, on 70 km/h, the app shows only about 15,5 km/h. For comparison, I'm using Navigon which shows correct speed. What went wrong?

Thanks to anyone in advance!
 

mitsusdev

Member
Licensed User
Longtime User
i have notice that GPS_LocationChanged function is called each 0,5 second.

is it correct ?

i need done a speed media :)
 
Status
Not open for further replies.
Top