B4A Library FusedLocationProvider

FusedLocationProvider is Android's latest attempt to improve the location services available to your applications.

Official documentation can be found here: https://developer.android.com/google/play-services/location.html.

This library depends on the Google Play Services library, android-support-v4 library and the GPS library.
The GPS library is required in order to use it's Location object.

FusedLocationProvider
Events:

  • ConnectionFailed (ConnectionResult1 As Int)
  • ConnectionSuccess
  • ConnectionSuspended (SuspendedCause1 As Int)
  • LocationChanged (Location1 As Location)
  • LocationSettingsChecked (LocationSettingsResult1 As LocationSettingsResult)
Fields:
  • ConnectionResult As ConnectionResult
    Contains the various ConnectionResult constants.
  • SuspendedCause As SuspendedCause
    Contains the various SuspendedCause constants.
Methods:
  • CheckLocationSettings (LocationSettingsRequest1 As LocationSettingsRequest)
    Checks if the relevant system settings are enabled on the device to carry out the desired location requests.
    Raises the event:
    LocationSettingsChecked(LocationSettingsResult1 As LocationSettingsResult)
  • Connect
    Attempt to connect to the Location Services.
    Will raise either event:
    ConnectionFailed(ConnectionResult1 As Int)
    ConnectionSuccess
  • Disconnect
    Disconnect from the Location Services.
  • GetLastKnownLocation As Location
    Returns the best most recent location currently available.
    Can only be called if the FusedLocationProvider is connected.
    The returned Location object will not be initialized if no last known location is available.
  • Initialize (EventName As String)
    Initialize the FusedLocationProvider object.
  • IsConnected As Boolean
    Returns whether the FusedLocationProvider is connected to the Location Services.
  • IsConnecting As Boolean
    Returns whether the FusedLocationProvider is trying to connect to the Location Services.
  • IsInitialized As Boolean
  • RemoveLocationUpdates
    Remove all requests for location updates.
  • RequestLocationUpdates (LocationRequest1 As LocationRequest)
    Request for location updates.
    The LocationRequest object defines the criteria for which location updates are requested.

This is the main library object.
You call the FusedLocationProvider Initialize method and then it's Connect method.
It will then raise the ConnectionFailed event or the ConnectionSuccess event.
Assuming the ConnectionSuccess event is raised you can now call:
  • GetLastKnownLocation As Location
  • RequestLocationUpdates (LocationRequest1 As LocationRequest)

So you could connect, get the last known location and then disconnect.
There is no requirement to request location updates.
This is a quick and simple way to get the device location.

Or you could connect then initialize and configure a LocationRequest object and then request location updates.
The LocationRequest object has various methods you can call to configure the request for location updates:

LocationRequest
Fields:

  • Priority As Priority
    Contains the various priority constants.
Methods:
  • GetExpirationTime As Long
    Get the request expiration time, in milliseconds since boot.
  • GetFastestInterval As Long
    Get the fastest interval of this request, in milliseconds.
  • GetInterval As Long
    Get the desired interval of this request, in milliseconds.
  • GetNumUpdates As Int
    Get the number of updates requested.
  • GetPriority As Int
    Get the quality of the request.
  • GetSmallestDisplacement As Float
    Get the minimum displacement between location updates in meters.
    By default this is 0.
  • Initialize
    Initialize the LocationRequest with default parameters.
    Default parameters are for a block accuracy, slowly updated location.
  • IsInitialized As Boolean
  • SetExpirationDuration (Millis As Long) As LocationRequest
    Set the duration of this request, in milliseconds.
  • SetExpirationTime (Millis As Long) As LocationRequest
    Set the request expiration time, in millisecond since boot.
  • SetFastestInterval (Millis As Long) As LocationRequest
    Explicitly set the fastest interval for location updates, in milliseconds.
  • SetInterval (Millis As Long) As LocationRequest
    Set the desired interval for active location updates, in milliseconds.
  • SetNumUpdates (NumUpdates As Int) As LocationRequest
    Set the number of location updates.
  • SetPriority (Priority As Int) As LocationRequest
    Set the priority of the request.
  • SetSmallestDisplacement (SmallestDisplacementMeters As Float) As LocationRequest
    Set the minimum displacement between location updates in meters.
    By default this is 0.

It is important to note that part of the criteria that defines your request for a location is the location permission that you (manually) set in the manifest file.
This library does not automatically add any permission to your manifest and this library will fail to work if you do not manually add a required permission to your manifest file.

You can add one of two permissions to your manifest:
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_COARSE_LOCATION

See: https://developer.android.com/training/location/retrieve-current.html
Apps that use location services must request location permissions.
Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION.
The permission you choose determines the accuracy of the location returned by the API.
If you specify ACCESS_COARSE_LOCATION, the API returns a location with an accuracy approximately equivalent to a city block.

Also note that as this library uses the Google Play Services library, you must also add this entry to your manifest:

B4X:
AddApplicationText(<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />)

Two versions of the library are attached.
  • If you're using a version of Google Play Services older than version 27 then you need to use FusedLocationProvider version 1.10.
  • If you're using Google Play Services version 27 or newer then you need to use FusedLocationProvider version 1.30 or newer

Martin.

Edit by Erel:
Add these two lines if using with B4A v6+:
B4X:
#AdditionalJar: com.android.support:support-v4
#AdditionalJar: com.google.android.gms:play-services-location
New example where FLP is managed from the starter service: https://www.b4x.com/android/forum/threads/fusedlocationprovider.50614/post-717726
 

Attachments

  • FusedLocationProvider_library_files_v1.10.zip
    19.3 KB · Views: 1,237
  • FusedLocationProvider_library_files_v1.31.zip
    20 KB · Views: 2,120
Last edited by a moderator:

Daniel-White

Active Member
Licensed User
same same,
I have the same problem as described in the previous post. Using B4A 6.00
I am not 100% sure, I remember had the same problem java.lang.NoClassDefFoundError
did you put those lines. in

B4X:
#Region  Project Attributes
    #ApplicationLabel:Unknow LoL
    #VersionCode: 1
    #VersionName: 9999999 and more who knows
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False

'Now for  B4A Ver 6.0
' For FusedLocation
#AdditionalJar: com.google.android.gms:play-services-location
'
 

DonManfred

Expert
Licensed User
If you need to use this lib with b4a 6+ you need to change the library XML file and rplace the dependson entries.
After removing the #additionalres from the sample app (post #2) the app compiles and run fine here on android 6 (S7 EDGE) and B4A 6.

I´ve added the changed files.

Please note that you need B4A 6+ for this libraryversion get to work as it is referencing maven-repository

<dependsOn>com.android.support:support-v4</dependsOn>
<dependsOn>com.google.android.gms:play-services-location</dependsOn>
 

Attachments

  • FusedLocationProvider_library_files_v1.31.zip
    19.9 KB · Views: 317
Last edited:

Anser

Well-Known Member
Licensed User
Can anyone confirm that whether any issue is there with this lib on the model "HTC Desire 816G Dual SIM" Phone

On this phone, Google Maps is showing the correct location, but in my app
LastLocation.IsInitialized = False always.

The same app is working on many different phones, so far I noticed complaint only on this particular phone.

Any advice will be appreciated.

Regards
Anser
 

Anser

Well-Known Member
Licensed User
Weird issue, send your zipped project in order to have a look

Shall try to build a self contained sample. Thank you.

Have you tried to update / install Google Play Service (on the device)?

As this was a client's phone, I just had this particular phone on my hand for hardly 10 to 15 minutes. During that time I connected the phone to my PC to see the logs. From the logs, I identified that the Location is not getting initialize. I couldn't check for the GooglePlay Services version. May be that can be a reason. Google Maps is working fine on that phone.

I have requested the phone for further investigation. Shall share the results here.
 

Anser

Well-Known Member
Licensed User
Can anyone confirm that whether any issue is there with this lib on the model "HTC Desire 816G Dual SIM" Phone

On this phone, Google Maps is showing the correct location, but in my app
LastLocation.IsInitialized = False always.

The same app is working on many different phones, so far I noticed complaint only on this particular phone.

Have you tried to update / install Google Play Service (on the device)?

Hi all,

Just wanted to confirm that the issue was due to the older version of Google Play Services installed on the device. Today, I had a chance to get the device on my hand once again and I updated "Google Play Services" on the device to the latest version and the problem resolved. Now it is working fine.

But, I still don't understand how come google maps is working fine on this device, may be it is not depended on "Google Play Services", I assume. I used to boast, that if Google Maps is able to show your current location in the map on their devices then my app would also work fine.

Regards

Anser
 

RVP

Active Member
Licensed User
I just need a bit of clarification on what is required for fused. It needs to have Google Play Services installed, but this is not a default app that is installed with later versions of Android? Google Play services also needs a google account to be logged in for it to work?

The reason I ask, is I have an app I developed for a client. I added GPS tracking to it, originally using the GPS library, and then changed it to fused location as it seemed it would be better suited, but now does not seem to function when installed on their phones. First because they did not have Google Play, and now I suspect because they do not have Ga oogle account.

If my above assumptions are correct, I need to go back to GPS services as the apps on these phones are installed remotely, and have to be usable without the user needing to go and create a Google account, and then sign in.
 

warwound

Expert
Licensed User
Google Play services also needs a google account to be logged in for it to work?

Yes that's correct.

I did some work a year back and used FusedLocationProvider only to find my client's devices won't be logged into Play Store.
I too had to revert to the default GPS library.
 

Rusty

Well-Known Member
Licensed User
In your LocationAPI library you included physical Activity Recognition.
The new FusedLocation library doesn't have this.
Is it your intent that one uses your Fused library for location and the ActivityRecognition separately to detect activities?
Rusty
 

Daniel-White

Active Member
Licensed User
Hi everybody.

DETECTING IN_VEHICLE, ON_BICYCLE , ON_FOOT, STILL, TILTING

I had been watching this video please take a look at minute 29:25 that video date from 2013.


can we use fusedlocation to detect if the user is in a vehicle, or walking?, I would like to detect that, because is the user is in a bus, the app can ask the location more frequently, and if the user is walking, the app can ask the location with less frequency. The accuracy I aM looking not to be super exact. only enough to know where is located the user in an emergency.
 

Ohanian

Active Member
Licensed User
Hi,

Is it possible to localize the StartResolutionDialog dialog then the gps is off?
 

FrankDev

Active Member
Licensed User
hi

im my application i use a progress like it is described in #2
(define a position, distance and a timer. (radius 200m, timer is 30 sec)
Get a 'LocationChanged' event-

from another task (second task) i need the actual position.
How to retreive the coordinates without disturbing the other process ?

Sometimes this task (second task) needs the position but the first process is not running and not initialized.

regards Frank
 

fishwolf

Well-Known Member
Licensed User
Example where FLP is managed in the starter service is attached.

i_view64_haiAKUit7X.png
When i move the app in background, the service is stopped
When i close tha app, the service start again

is it possible tracking the gps coordinate in background?
 
Top