fusedLocationProviderClient.lastLocation.addOnSuccessListener always null

后端 未结 2 875
庸人自扰
庸人自扰 2021-02-19 20:25

I just updated my Location API to use FusedLocationProviderClient but I am having this issue, when I turn off and on the GPS, I am always getting null location:

         


        
相关标签:
2条回答
  • 2021-02-19 21:02

    I found a solution, this is what happens, when the location is null it means the location cache was cleared, this happens when turning off GPS, so when I was turning it on there was no last location to get, what I did was this:

    checkLocationSettings(callingActivity, turnOnGpsRequestCode, callback) {
                    // Location settings successful
                    mFusedLocationProviderClient!!.lastLocation
                            .addOnSuccessListener(callingActivity) {
                                location ->
                                if (location == null || location.accuracy > 100) {
                                    mLocationCallback = object : LocationCallback() {
                                        override fun onLocationResult(locationResult: LocationResult?) {
                                            stopLocationUpdates()
                                            if (locationResult != null && locationResult.locations.isNotEmpty()) {
                                                val newLocation = locationResult.locations[0]
                                                callback.onCallback(Status.SUCCESS, newLocation)
                                            } else {
                                                callback.onCallback(Status.ERROR_LOCATION, null)
                                            }
                                        }
                                    }
    
                                    mFusedLocationProviderClient!!.requestLocationUpdates(getLocationRequest(),
                                            mLocationCallback, null)
                                } else {
                                    callback.onCallback(Status.SUCCESS, location)
                                }
                            }
                            .addOnFailureListener {
                                callback.onCallback(Status.ERROR_UNKNOWN, null)
                            }
                }
    

    When the location is null, start requesting locations using a callback and

    mFusedLocationProviderClient!!.requestLocationUpdates(getLocationRequest(), mLocationCallback, null)

    Then when the callback is called, a new location is got and it starts getting location again.

    Sometimes it happens that when you turn on the GPS, the location is not null but the accuracy is bad, so I also check if location accuracy is good enough (For me good enough is 100 meters)

    0 讨论(0)
  • 2021-02-19 21:05

    You can use getLocationAvailability() method on your FusedLocationPrivedClient object and if it returns true then only use getLastLocation() method otherwise use requestLocationUpdates() method like this :

    FusedLocationProviderClient fusedLocationProviderClient =
                LocationServices.getFusedLocationProviderClient(InitActivity.this);
        if (ActivityCompat.checkSelfPermission(this.getApplicationContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
    
    fusedLocationProviderClient.getLocationAvailability().addOnSuccessListener(new OnSuccessListener<LocationAvailability>() {
                    @Override
                    public void onSuccess(LocationAvailability locationAvailability) {
                        Log.d(TAG, "onSuccess: locationAvailability.isLocationAvailable " + locationAvailability.isLocationAvailable());
                        if (locationAvailability.isLocationAvailable()) {
                            if (ActivityCompat.checkSelfPermission(InitActivity.this.getApplicationContext(),
                                    android.Manifest.permission.ACCESS_FINE_LOCATION)
                                    == PackageManager.PERMISSION_GRANTED) {
                                Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();
                                locationTask.addOnCompleteListener(new OnCompleteListener<Location>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Location> task) {
                                        Location location = task.getResult();                                    
                                    }
                                });
                            } else {
                                requestLocationPermissions ();
                            }
    
                        } else {
    fusedLocationProviderClient.requestLocationUpdates(locationRequest, pendingIntent);
    
                        }
    
                    }
                });
            } else {
                requestLocationPermissions ();
            }
    
    0 讨论(0)
提交回复
热议问题