microg / GmsCore

Free implementation of Play Services

Home Page:https://microg.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use FusedLocationProviderClient for getting location?

iFish85 opened this issue · comments

Hi,
I have installed latest release GmsCore to my device (Android S without GMS), and already granted all necessary permissions.

I wrote two test functions in my test app:
Use LocationManager can work fine.

fun getLocation1() {
    if (ActivityCompat.checkSelfPermission(
            this,
            permission.ACCESS_FINE_LOCATION
        ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
            this,
            permission.ACCESS_COARSE_LOCATION
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        return
    }
    var location = locationManager!!.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
    if (location != null) {
        Log.e(tag, "lat: ${location.latitude} lon: ${location.longitude}")
    }
}

But use FusedLocationProviderClient always throw exception:

fun getLocation2() {
    if (ActivityCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return
    } else {
        val mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
        mFusedLocationClient.lastLocation.addOnCompleteListener {
            val location = try {
                it.result
            } catch (ex: Exception) {
                Log.e(tag, "Cannot get location result $ex")
                null
            }

            if (location != null)
              Log.e(tag, "lat: ${location.latitude} lon: ${location.longitude}")
        }
    }
}
com.example.locationtest requires the Google Play Store, but it is missing.
The service for com.google.android.gms.internal.location.zzda is not available: ConnectionResult{statusCode=SERVICE_INVALID, resolution=null, message=null}
Cannot get location result com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ApiException: 17: API: LocationServices.API is not available on this device. Connection failed with: ConnectionResult{statusCode=SERVICE_INVALID, resolution=null, message=null}

I am using play-services-location 21.0.1
implementation 'com.google.android.gms:play-services-location:21.0.1'

Did I miss anything?