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

SCIS

Active Member
Licensed User
Longtime User
Are you checking it outdoors? GPS receivers mostly don't work indoors.

@Danbicky please start a new thread for this question.

Managed to get it working now. Thanks, Erel!

Really unfortunate for the poor connection indoors because there is a possibility that users also want to get their location when they're not outdoors (with the app we are designing).
Googlemaps app itself works perfectly indoors though.

Is there any way to get it working indoors?

Thanks in advance!
 

Gentry

Member
Licensed User
Longtime User
What is the dataype of the GPS Location object? It apparently is not one of these types : Lists, Arrays, Maps, Strings, primitive types and user defined types, as it does not write to file when used in an list element via RandomAccessFile.WriteObject. It displays as this:

B4X:
(ArrayList) [Location[gps 41.908098,-88.209641 acc=14 et=+1d9h1m25s31ms alt=201.0 vel=0.0 {Bundle[mParcelledData.dataSize=40]}]]

And I am guessing that the {Bundle...} portion is a problem.

If you needed to save a list of locations, and RandomAccessFile is not an option, is there an alternative?

My challenge is that Location is part of a user defined datatype(UDD), and I am attempting to save a list of this UDD to file, and read it back. When the list of UDD is read back, the type of the location element is lost (all values are null), and when I try to initialize2(lat,lon) the location type element, it fails, and if I try to check if it isInitialized (as a Location) I get this error:

B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean anywheresoftware.b4a.gps.LocationWrapper.IsInitialized()' on a null object reference

I was unable to find a new thread on this subject, and the question was posted in this thread previously, so I am attempting to ask the question in a way that is applicable to the main subject of this thread.

Thanks,
Gentry
 

GMan

Well-Known Member
Licensed User
Longtime User
It is always better to start a new thread.

You will need to create custom types and convert between Location to the custom type. For further discussion please start a new thread.

Hoi Erel,
it would be really great if the link to the new thread was published here.
Now i read 2 sides of comments and - now its broken 'cause i dont know for what to search for ,-)
Mayba a hint for further Board-Relaeses or so...
 

jchal

Active Member
Licensed User
Longtime User
hi all
is it possible to convertme this example to a service runing in bacground at a spesifc interval e.g ever10 miniutes? so i can understand how it works?
 

Declan

Well-Known Member
Licensed User
Longtime User
When I try and load this example, I get the following error:
B4A version: 5.20
Parsing code. Error
Error parsing program.
Error description: Unknown type: runtimepermissions
Are you missing a library reference?
Occurred on line: 7 (Starter)
Public rp As RuntimePermissions
 

Declan

Well-Known Member
Licensed User
Longtime User
Many thanks, sorted
As always, support is Awesome - Anywhere Software has to be the most service/support orientated supplier.
 

Rusty

Well-Known Member
Licensed User
Longtime User
I'm trying to compile the GPS example (post 1).
I'm using B4a 6.30 and it compiles and tries to install.
On my device (Samsung Galaxy S7), it merely states "App not installed."
Any ideas? (My manifest has the above (post 276) line in it.)
Thanks,
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
I did. I changed it several different times with the same result.
Any other ideas?
All i'm trying to do is create a GPS Service. Are there other examples from which I can start?
Thanks!
 
Last edited:
Status
Not open for further replies.
Top