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.

Ersen Osman
  • 7,067
  • 8
  • 47
  • 80

1 Answers1

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.

Ersen Osman
  • 7,067
  • 8
  • 47
  • 80
  • 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. – Subho Feb 17 '17 at 13:50
  • @Ersen Osman Thanks brother you are save my life.....Thank you once again – Gowthaman M Jan 22 '18 at 11:24
  • 1
    Is there any way to improve onLocationResult? Some of devices that has poor connection will never call it. – paugoo Aug 29 '18 at 09:32
  • 1
    Also onLocationAvailablity only being called 2 times, first return True, and second one is False, then never being called again. – paugoo Aug 29 '18 at 09:35
  • 2
    Can you tell me what you did to remove all existing location history (to prevent cached locations)? – Joshua Mar 18 '19 at 10:10
  • 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? – Mohamad Mousheimish Apr 19 '20 at 17:51