Android-ReactiveLocation: call() not executed

484 Views Asked by At

I am trying to use Android-ReactiveLocation by mcharmas.

Nothing is happening, i.e. it never gets to the call method. Location is turned on for the device. Using a GPS app, I can see that GPS is working. In Settings > Location > Recent location request, my app is first in the list. There are no error messages.
I must be missing something. What is it?

I include Reactive Location in the app build.gradle like so:

compile 'pl.charmas.android:android-reactive-location:0.10@aar'

I created an API key on Google Play and put it in the manifest file:

<meta-data
    android:name="com.google.android.geo.API_KEY"
     android:value=aaaaaaaaaaaa-bbbbbbbbbbbbbb_ccccccccccc"/>

In the gradle.properties file of my project, I added:

REACTIVE_LOCATION_GMS_API_KEY="aaaaaaaaaaaa-bbbbbbbbbbbbbb_ccccccccccc"

MainActivity.OnCreate():

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
        PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
            LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION);
}

locationProvider = new ReactiveLocationProvider(getApplicationContext());
lastKnownLocationObservable = locationProvider.getLastKnownLocation();

final LocationRequest locationRequest = LocationRequest.create()
        .setPriority(LocationRequest.PRIORITY_LOW_POWER)
        .setInterval(1000); // milliseconds

Subscription locationUpdateSubscription = locationProvider
        .getUpdatedLocation(locationRequest)
        .subscribe(new Action1<Location>(){
            @Override
            public void call(Location location){
                Log.i(TAG, "call: (" + location.getLatitude() + ", " + location.getLongitude() + ")");
                PreferencesUtilities.setCurrentLocation(mActivity.getApplicationContext(), location);
            }
        }
);
1

There are 1 best solutions below

0
On BEST ANSWER
Observable<Location> locationObservable = reactiveLocationProvider.getUpdatedLocation(
        LocationRequest.create()
        .setPriority(LocationRequest.PRIORITY_LOW_POWER)
        .setInterval(1000);

locationObservable.map(location -> {
     //do whatever you want with your location
}).subscribe();

this will works, off course adapt it with what you need