1

I am making an app using fusedLocationProviderClient to locate the user.

First, I try to retrieve last known location and then, if it isn't available I am making a request location.

Here is the issue, when I run the app, it doesn't trigger onLocationResult() method from the Callback.

So here's the logic of the code :

  • I tried to get the last known location, if it returns null, the method "checkIfCurrentLocationSettingsSatisfied" is summoned.

  • This method verifies if the phone's location is enabled, if not an other activity is launched and asks the user to allow location. The result code is checked on the "onActivityResult" method.

  • Furthermore, the location request is initiated. After that, the location Callback is also initiated.

  • Last of all, the fusedLocationProviderClient variable is set with the location request and the callback.

When the phone has a last known location, there is no problem. It's only when it tries to get a location.

In the manifest, I gave the " android.permission.ACCESS_FINE_LOCATION " permission. I found a topic that has an issue like mine but I tried every solutions proposed and it hasn't resolving my issue. Here is the topic : LocationCallback not getting called

Here is the code :

@Override
public void onMapReady(GoogleMap googleMap) {
    this.googleMap = googleMap;
    googleMap.setMyLocationEnabled(true);
    googleMap.setOnMapLoadedCallback(this);
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);
    googleMap.setOnMarkerClickListener(this);
    getDeviceLocation();
}

private void getDeviceLocation(){
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
    try{
        if(mLocationPermissionGranted){
            Task location = fusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(task -> {
                if(task.isSuccessful()){
                    Location currentLocation = (Location) task.getResult();
                    if(currentLocation == null){
                        builder = new LocationSettingsRequest.Builder();
                        checkIfCurrentLocationSettingsSatisfied();
                    }else{
                        moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));
                    }
                }else{
                    Toast.makeText(getContext(), R.string.couldnt_get_location, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }catch (SecurityException e){
        e.printStackTrace();
    }
}

private void moveCamera(LatLng latLng){
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM));
}

public void checkIfCurrentLocationSettingsSatisfied(){
    SettingsClient client = LocationServices.getSettingsClient(getActivity());
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
    task.addOnSuccessListener(locationSettingsResponse -> {
        createLocationRequest();
    });

    task.addOnFailureListener(getActivity(), e -> {
        if (e instanceof ResolvableApiException) {
            try {
                ResolvableApiException resolvable = (ResolvableApiException) e;
                startIntentSenderForResult(resolvable.getResolution().getIntentSender(), REQUEST_CHECK_SETTINGS, null, 0, 0, 0, null);
            } catch (IntentSender.SendIntentException sendEx) {
                sendEx.printStackTrace();
            }
        }
    });
}

private void createLocationRequest() {
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(5000);
    locationRequest.setFastestInterval(2000);
    initializeLocationCallback();
}

private void initializeLocationCallback() {
    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                Toast.makeText(getContext(), R.string.couldnt_get_location, Toast.LENGTH_SHORT).show();
                return;
            }
            for (Location location : locationResult.getLocations()) {
                // It never gets here
                moveCamera(new LatLng(location.getLatitude(),location.getLongitude()));
            }
        }
    };
    startLocationUpdates();
}

private void startLocationUpdates() {
    fusedLocationProviderClient.requestLocationUpdates(locationRequest,
            locationCallback,
            Looper.getMainLooper());
}
Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
Gus_2106
  • 11
  • 2
  • Is the `if(mLocationPermissionGranted){...}` condition true i.e. you have asked for the permission at runtime and it's allowed by the user? The `LocationRequest.PRIORITY_HIGH_ACCURACY` will require GPS, so if testing on a real device you likely need to go outside or use a "mock location" app. If testing on the emulator, you'll need to simulate a location with the emulator tools. – Markus Kauppinen Jun 10 '20 at 18:11
  • Yes, the app ask the user's permission to use location when it's started for the first time. i authorized it. When i run the Google Maps app and come back to the app, it gets a location. I don't know if it's a lastknownlocation or a location request. – Gus_2106 Jun 11 '20 at 08:52
  • Google Maps will update the location, so after running Maps you'll have a "last known location". Sounds like you are not getting a location callback in your app. Like said, go out if testing on a real device or play with the emulator location tools if emulating. – Markus Kauppinen Jun 11 '20 at 10:01
  • ...or you can additionally request updates with [some of the lower priorities](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest#PRIORITY_BALANCED_POWER_ACCURACY) like `PRIORITY_BALANCED_POWER_ACCURACY` as they don't require GPS. – Markus Kauppinen Jun 11 '20 at 17:38
  • I went out earlier to try the app and get a location, it didn't worked. I already tried to get location with different priority like "PRIORITY_BALANCED_POWER_ACCURACY" . It still didn't get any location. So tommorow, I'm going to make a new app and try to only get the user location. Maybe i'll find what went wrong ... – Gus_2106 Jun 11 '20 at 18:14

0 Answers0