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: 2,186
  • FusedLocationProvider_library_files_v1.31.zip
    20 KB · Views: 3,251
Last edited by a moderator:

Daniel-White

Active Member
Licensed User
Longtime User

Sub Process_Globals
Public FusedLocationProvider1 As FusedLocationProvider
Public GPSoWifiTorres As LocationSettingsStates
End Sub

Sub Testing
if GPSoWifiTorres.IsGpsUsable=True then etc etc etc.
End If

I am stuck, I did not see how to initialize the variable GPSoWIFItorres .
upload_2016-3-11_9-25-55.png


Thanks youuuuuuuuuuuuu
 

warwound

Expert
Licensed User
Longtime User
If LocationSettingsStates has no Initialize method then you cannot create an instance of the object yourself.
You'll probably find that an instance of this object is returned by one of the library methods.

Search the FusedLocationProvider.html file (this html file is part of the library download) for LocationSettingsStates.
Can you find a method that returns an instance of this object?
 

Daniel-White

Active Member
Licensed User
Longtime User
If LocationSettingsStates has no Initialize method then you cannot create an instance of the object yourself.
You'll probably find that an instance of this object is returned by one of the library methods.

Search the FusedLocationProvider.html file (this html file is part of the library download) for LocationSettingsStates.
Can you find a method that returns an instance of this object?

Thanks you so much warwound, I put it to work after sweat a little bit. :eek:

However, I discovered I took the wrong route :mad:, my fault, because I am looking the FusedProvider tell me if the lastest LAT and LONG was obtained using GPS or (Wifi , cell towers). with the LocationSettingsStates only say the device is able to use the GPS and (wifi towers).

plop!!! o_O
Thanks you, I learn something new today :cool:
 

Johan Schoeman

Expert
Licensed User
Longtime User
Can't get the project in post #2 of this thread to compile. I have
1. B4A v5.80 installed
2. google-play-services.jar (the latest jar) in my additional lib folder (google play services Rev 29 installed)
3. Path to the "res" folder set correctly
4. FusedLocationProvider V1.31 enabled
5. GPS V1.2 enabled.

It starts compiling and then without any errors eventually times out. I have set the timeout to 60 seconds but it makes no further progress with compiling the B4A project than what it does when the timeout is set to 30 seconds.

Anything that I am missing here to get it to work?

1.png




2.png
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Maybe just a typo error in ANDRIOD_SDK_TOOLS ?
No - that is how my folder is named (made the typo when I first created the folder). ANDRIOD_SDK_TOOLS is correct. If I change it to ANDROID_SDK_TOOLS it generates an error right away when I try to compile the project.
 

rboeck

Well-Known Member
Licensed User
Longtime User
Ok, i tried the same configuration, my Setinterval is set to 1000. As long as the position is not changing, i only get an update one or two time in a minute; if i walk, the updates are coming faster, but its not easy to find a rule. The shortest updates are 5 sec. other are 30 sec. Which parameter did you use - there are a lot of possibilities?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Ok, i tried the same configuration, my Setinterval is set to 1000. As long as the position is not changing, i only get an update one or two time in a minute; if i walk, the updates are coming faster, but its not easy to find a rule. The shortest updates are 5 sec. other are 30 sec. Which parameter did you use - there are a lot of possibilities?
I am still just trying to get it to compile - I am using the project in post #2 of this thread as is (other than changing the path to the res folder. It wont compile - starts and then just stops as per pic in post #124. But in the sample project of post #2 the SetInterval is set to 1000. I first need to get it to compile. As I have said, no errors whatsoever - it just eventually times out before completing the compilation of the B4A project
 

rboeck

Well-Known Member
Licensed User
Longtime User
Sorry, i didnt read your post carefully - i now checked the timing for converting byte code - optimized dex. It's only 1.58 sec. All versions are the same, only the fusedLocationProvider is on version 1.3.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Sorry, i didnt read your post carefully - i now checked the timing for converting byte code - optimized dex. It's only 1.58 sec. All versions are the same, only the fusedLocationProvider is on version 1.3.
OK - this solved it:
B4X:
#ExcludeClasses: .games, .drive, .ads, .fitness, .wearable, .measurement, .cast, .auth, .nearby
#ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .vision, .gcm, .maps, .panorama
 
Last edited:

Alisson

Active Member
Licensed User
I not found the com.google.android.gms in my folder.

I use the SDK Manager, but not show option to install the com.google.android.gms.
How can I do?

Thanks
 

Attachments

  • com.google.gms.png
    com.google.gms.png
    59.9 KB · Views: 330

Alisson

Active Member
Licensed User
Erel, thanks to answer.

But I installed when I created the environment for B4A, look:


upload_2016-5-13_10-7-47.png


What I did wrong ?
 

bryantm1122

Member
Licensed User
I followed the tutorial Erel linked above (https://www.b4x.com/android/forum/threads/google-play-services-installation-tips.45843/#content) and installed Google Play Services through SDK Manager. Looking in the android-sdk\extras\google\google_play_services directory, i have no libproject folder:

4eaVTXh.jpg


I'm showing hidden files and folders and using Google Play Services version 30. Updated all other packages with updates as well.

If anyone has any thoughts, I greatly appreciate it.
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
If anyone has any thoughts, I greatly appreciate it.
GPS has disappeared for me here too after upgrade to version #30

You will find them in your Android's SDK folder elsewhere : \extras\google\m2repository\com\google\android\gms
but in .aar format so you'll have to extract it with 7Zip for example

I have found no .jar inside the archive. Perhaps downloading them from here: http://www.smorgasbork.com/2015/01/05/google-play-services-sdk-version-history

Seems to be all OK with that link to version #29: https://dl-ssl.google.com/android/repository/google_play_services_8487000_r29.zip
 
Last edited:

bryantm1122

Member
Licensed User
GPS has disappeared for me here too after upgrade to version #30

You will find them in your Android's SDK folder elsewhere : \extras\google\m2repository\com\google\android\gms
but in .aar format so you'll have to extract it with 7Zip for example

I have found no .jar inside the archive. Perhaps downloading them from here: http://www.smorgasbork.com/2015/01/05/google-play-services-sdk-version-history

Seems to be all OK with that link to version #29: https://dl-ssl.google.com/android/repository/google_play_services_8487000_r29.zip

I found this inside the \gms\play-services-location\9.0.0 .aar file when I unzipped:

UYpqdaL.jpg


I'm assuming this is the jar for the location library.

Thanks for the link to version 29. I'll check it out to get rolling, but I like the idea of only using jars for the modules I need. Version 30 seems like another way to do this: https://www.b4x.com/android/forum/t...-apis-into-b4a-application.61537/#post-388268
 

Nickelgrass

Active Member
Licensed User
Longtime User
Hi,
I am getting errors when using the library:

Installing file.
PackageAdded: package:run.gearloose.net
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (tracker) Create **
java.lang.NoClassDefFoundError: uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProvider
at uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProviderWrapper.Initialize(FusedLocationProviderWrapper.java:173)
at run.gearloose.net.tracker._service_create(tracker.java:360)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at run.gearloose.net.tracker.onCreate(tracker.java:53)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2574)
at android.app.ActivityThread.access$1800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Unable to create service run.gearloose.net.tracker: java.lang.RuntimeException: java.lang.NoClassDefFoundError: uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProvider
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2584)
at android.app.ActivityThread.access$1800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.NoClassDefFoundError: uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProvider
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:165)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at run.gearloose.net.tracker.onCreate(tracker.java:53)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2574)
... 10 more
Caused by: java.lang.NoClassDefFoundError: uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProvider
at uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProviderWrapper.Initialize(FusedLocationProviderWrapper.java:173)
at run.gearloose.net.tracker._service_create(tracker.java:360)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
... 13 more
Any idea what it could be? I added all the libs and permissions needed.

Thanks!
 

eurojam

Well-Known Member
Licensed User
Longtime User
same same,
I have the same problem as described in the previous post. Using B4A 6.00
 
Top