FusedLocationClient not calling onLocationResult

11.6k Views Asked by At

I'm creating a location client at the moment, currently i have

 public void startUpdatingLocationProcess(Context context) {

     parentContext = context;

    if (mFusedLocationClient == null){
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(parentContext);

        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (ContextCompat.checkSelfPermission(parentContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, new LocationCallback(){
                @Override
                public void onLocationResult(LocationResult locationResult) {

                    onLocationChanged(locationResult.getLastLocation());
                }

            } , Looper.myLooper());

         }


}

This is in a LocationApi class that i want to run throughout the applications lifecycle, this particular app runs many activities so i don't want to "recreate" the request every time i destroy and create a new activity.

The permission for FINE_LOCATION is allowed and checked, there are no errors in the logcat and the code runs the requestLocationUpdates method on creation of the mFusedLocationClient object, but it never calls the "onLocationResult" method.

Is there something i'm missing with using this api?

3

There are 3 best solutions below

10
On BEST ANSWER

Before requesting location updates, your app must connect to location services and make a location request. Some thing like this:

private static LocationRequest createLocationRequest() {
    LogHelper.trace("createLocationRequest");
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(200000);
    mLocationRequest.setFastestInterval(300000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    return mLocationRequest;
}

public static void checkLocationService(final Fragment fragment, final FusedLocationProviderClient client, final OnSuccessListener<LocationSettingsResponse> successListener, OnFailureListener failureListener) {

    LogHelper.trace("checkLocationService");
    final LocationRequest request = createLocationRequest();
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(request);

    SettingsClient settingsClient = LocationServices.getSettingsClient(fragment.getActivity());
    Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(builder.build());

    task.addOnSuccessListener(fragment.getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            LogHelper.trace("onSuccess");
            startLocationService(client, request, new LocationCallback());
            successListener.onSuccess(locationSettingsResponse);
        }
    });

    task.addOnFailureListener(fragment.getActivity(), failureListener);
}
0
On

try to active/desactive location from android settings.

1
On

location == null.

See also LocationAvailability No Location Available. If you call

fusedLocationClient.getLocationAvailability()
    .addOnSuccessListener(this, locationAvailability -> {
    })

you will see that LocationAvailability[isLocationAvailable: false].

Probably it will happen because of an old emulator or

locationRequest = LocationRequest.create();
locationRequest
        .setNumUpdates(1)
        .setFastestInterval(0)
        .setSmallestDisplacement(0)

If you set setNumUpdates(1), it will return coordinate only one time and probably there will be null. I removed these lines and use

@SuppressLint("MissingPermission")
private fun startLocationUpdates() {
    fusedLocationProviderClient?.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
}

to start search location, then

locationCallback = object : LocationCallback() {
    override fun onLocationResult(locationResult: LocationResult?) {
        super.onLocationResult(locationResult)

        if (locationResult?.lastLocation == null) {
            Timber.d("Location missing in callback.")
        } else {
            Timber.d(
                "Location Callback ${locationResult.lastLocation}")
            latitude = locationResult.lastLocation.latitude
            longitude = locationResult.lastLocation.longitude
            stopLocationUpdates()
        }
    }
}

fusedLocationProviderClient?.lastLocation
    ?.addOnSuccessListener { location ->
        Timber.d("lastLocation success $location")
        if (location == null) {
            startLocationUpdates()
        } else {
            latitude = location.latitude
            longitude = location.longitude
        }
    }
    ?.addOnFailureListener { failure ->
        Timber.d("lastLocation failure ${failure.message}")
    }

to receive coordinate.