1

I am using FusedLocationProvider to get the last location of the phone. Unfortunately the returned location is far from being accurate and it is not updating!.

Even When I call requestlocationupdates,the OnlocationResult never being called with PRIORITY_HIGH_ACCURACY. BUT it called ONLY once with PRIORITY_BALANCED_POWER_ACCURACY.

protected void startLocationUpdates() {

    // Create the location request to start receiving updates
    locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    locationRequest.setInterval(5000);
    locationRequest.setFastestInterval(1000);
    // Create LocationSettingsRequest object using location request
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(locationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();

    // Check whether location settings are satisfied
    // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    Task<LocationSettingsResponse> task =settingsClient.checkLocationSettings(locationSettingsRequest);

    task.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...
            Log.i("SRV", "onSuccess: Location settings are satisfied");
        }
    });

    task.addOnFailureListener( new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            if (e instanceof ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                Log.i("SRV", "onFailure: ");
            }
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            initialized = false;
            //return;
        }
    }

    // new Google API SDK v11 uses getFusedLocationProviderClient(this)
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    mFusedLocationClient.requestLocationUpdates(locationRequest, new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    super.onLocationResult(locationResult);
                    Log.i("SRV", "onLocationResult is called ");
                    if (locationResult == null) {
                        return;
                    }
                    for (Location location : locationResult.getLocations()) {
                        if (location != null) {
                            Log.i("SRV", "My new location is " + location.getLatitude() + " " + location.getLongitude());
                            onLocationChanged(location);
                        }
                    }
                }

                @Override
                public void onLocationAvailability(LocationAvailability locationAvailability) {
                    Log.i("SRV", "onLocationAvailability is called ");
                }

            },
            Looper.myLooper());

    //get last location
    mFusedLocationClient.getLastLocation().addOnSuccessListener(location -> {
        if (location != null) {
            _phoneLocation = location;
            Log.i("SRV", "getLastLocation is called :  My position " + location.getLatitude() + " " + location.getLongitude());
        }
    });
    Log.i("SRV", "Startlocation updated was completed succesfully");
}

I expect the OnlocationResult to be called and updated very 5 seconds.

Raafat
  • 11
  • 3

1 Answers1

0

It turns out that it is a stupid thing. We spent too much time on this. I had some issues of checking the permissions and allowing it.
Here I post the final code that works perfectly. Make sure to allow the location permission when it prompts. If you are debugging many times, It is recommended to uninstall the app first to allow the permission again.

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION},
                1);
    } else if (mMap != null) {
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(true);
    }
Raafat
  • 11
  • 3