skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

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


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

    Edit:
    Request location permissions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search