Fused Location Provider: Is there a way to check if a location update failed?
Asked Answered
V

1

15

Introduction

In my app I want to get a one-off accurate location of where the user currently is. When I used FusedLocationProviderApi.getLastLocation sometimes this would be null or out of date location because I found out this just gets a cached location and does not request a new location update.

The solution was to request a location update only once as seen below.

 LocationRequest locationRequest  = LocationRequest.create()
                .setNumUpdates(1)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(0);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this)

Now I get a more accurate location all the time.

My Question

How can I determine if a location update failed? Since I am only requesting 1.

When a location update is obtained this callback is invoked

 @Override
 public void onLocationChanged(Location location) {

 }

However this does not get called if a location update failed.

What I have tried

I saw there was a ResultCallback. However, onSuccess seems to be always called even if the one-off location update failed.

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this).setResultCallback(new ResultCallbacks<Status>() {
            @Override
            public void onSuccess(@NonNull Status status) {
                DebugUtils.log("requestLocationUpdates ResultCallback onSuccess + " + status.toString(),true,true);
            }

            @Override
            public void onFailure(@NonNull Status status) {
                DebugUtils.log("requestLocationUpdates ResultCallback onFailure + " + status.toString(),true,true);
            }
        });

Other

Using com.google.android.gms:play-services-location:8.4.0

Thanks for reading, please help me out.

Violative answered 6/3, 2016 at 18:48 Comment(2)
Tried checking the status parameter of onSuccess() method?Expectorant
Yes, the toString method reports "Status{statusCode=SUCCESS, resolution=null}" when it should failViolative
V
34

After digging around the API a little more and doing some tests, I found a solution to my problem which seems to be working.

 LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult result) {
                DebugUtils.log("onLocationResult");
            }

            @Override
            public void onLocationAvailability(LocationAvailability locationAvailability) {
                DebugUtils.log("onLocationAvailability: isLocationAvailable =  " + locationAvailability.isLocationAvailable());
            }
        }, null);

Instead of using LocationListener. I used LocationCallback

onLocationResult is only called to provide the latest Location result based on the LocationRequest. It is not called if a location could not be provided and during my tests it is not when Location was disabled and GPS was used indoors etc. Please call result.getLastLocation() to get the actual location object.

onLocationAvailablity is always called. locationAvailability.isLocationAvailable() Returns true if the device location is known and reasonably up to date within the hints requested by the active LocationRequests. False is returned when failure to determine location may from a number of causes including disabled location settings or an inability to retrieve sensor data in the device's environment.

I have tested with no location services, GPS only while indoors. In each case I removed all existing location history (to prevent cached locations). An accurate up-to-date location was always returned.

Could others vouch for this?

Note: The last parameter for the requestLocationUpdates call is a looper. Passing in null means it will use the calling thread's message queue.

Violative answered 6/3, 2016 at 22:43 Comment(6)
You know something for this i spend 2 whole days. I will be sharing this answer as much as possible. It should be marked as accept answer.Glenoid
@Ersen Osman Thanks brother you are save my life.....Thank you once againMiss
Is there any way to improve onLocationResult? Some of devices that has poor connection will never call it.Cassandracassandre
Also onLocationAvailablity only being called 2 times, first return True, and second one is False, then never being called again.Cassandracassandre
Can you tell me what you did to remove all existing location history (to prevent cached locations)?Anthonyanthophore
I'm using fusedLocationClient.requestLocationUpdates(new LocationRequest(), locationCallback, Looper.getMainLooper());. But the location is always being returned the same even when I'm walking. You have any idea about it?Cagliostro

© 2022 - 2024 — McMap. All rights reserved.