Home > Software design >  Missing permission when trying to get current location using fusedLocationClient
Missing permission when trying to get current location using fusedLocationClient

Time:06-23

Good afternoon! I'm trying to get geolocation using this official guide: link As written in the guide, I added this at the beginning of the class:

private FusedLocationProviderClient fusedLocationClient;

Also I added this code in onCreateView:

fusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());

    fusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.
                    if (location != null) {
                        longText.setText("Longitude: "   Double.toString(location.getLongitude()));
                        latText.setText("latitude: "   Double.toString(location.getLatitude()));
                    }
                }
            });

I added this to the manifest:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

And I added this in build.gradle:

implementation 'com.google.android.gms:play-services-location:20.0.0'

But I am getting two errors:

Missing permissions required by FusedLocationProviderClient.getLastLocation: android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION
Cannot resolve method 'addOnSuccessListener(com.maxet24.chargely.ListFragment, anonymous com.google.android.gms.tasks.OnSuccessListener<android.location.Location>)'

Here is the whole code: ListFragment.java https://pastebin.com/bU5Ek80K
fragment_list.xml: https://pastebin.com/zsMuT1Pe
AndroidManifest.xml: https://pastebin.com/J505ALA7

CodePudding user response:

Do you grant those 2 permission (ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION) before calling mFusedLocationProviderClient.getLastLocation()?

Edit: Request location permissions

CodePudding user response:

I just ignored this error and the program works fine on the phone.
Alt Enter -> Suppress
But still, it's weird.

  • Related