flutterlocation icon indicating copy to clipboard operation
flutterlocation copied to clipboard

Resolvable API exception for getting location

Open ajinkya446 opened this issue 1 year ago • 0 comments

Description Could not able to get latitude and longitude position for android/iOS devices.

Expected behavior Should get the latitude and longitude for the current location using GPS.

Tested on:

  • Android, All the devices whose API level is more than 21
  • iOS

While debugged the package code for getting latitude and longitude got this exception message for the below code com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED

`public void startRequestingLocation() {
        if (this.activity == null) {
            result.error("MISSING_ACTIVITY", "You should not requestLocation activation outside of an activity.", null);
            throw new ActivityNotFoundException();
        }
        mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
                .addOnSuccessListener(activity, locationSettingsResponse -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        locationManager.addNmeaListener(mMessageListener, null);
                    }

                    if (mFusedLocationClient != null) {
                        mFusedLocationClient
                                .requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
                    }
                }).addOnFailureListener(activity, e -> {
                    if (e instanceof ResolvableApiException) {
                        ResolvableApiException rae = (ResolvableApiException) e;
                        int statusCode = rae.getStatusCode();
                        if (statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                            try {
                                // Show the dialog by calling startResolutionForResult(), and check the
                                // result in onActivityResult().
                                rae.startResolutionForResult(activity, REQUEST_CHECK_SETTINGS);
                            } catch (IntentSender.SendIntentException sie) {
                                Log.i(TAG, "PendingIntent unable to execute request.");
                            }
                        }
                    } else {
                        ApiException ae = (ApiException) e;
                        int statusCode = ae.getStatusCode();
                        if (statusCode == LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE) {// This error code happens during AirPlane mode.
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                locationManager.addNmeaListener(mMessageListener, null);
                            }
                            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
                                    Looper.myLooper());
                        } else {// This should not happen according to Android documentation but it has been
                            // observed on some phones.
                            sendError("UNEXPECTED_ERROR", e.getMessage(), null);
                        }
                    }
                });
    }
}`

It is directly executing the on failure method while requesting the location.

ajinkya446 avatar Aug 04 '23 08:08 ajinkya446