获取最近的已知位置ACCESS_COARSE_LOCATION +没有Google服务

问题描述

是否有可能获得Google服务之类的最近的已知位置?FusedLocationClient仅在获得ACCESS_COARSE_LOCATION权限的情况下执行此操作,而没有Google服务吗?

我尝试了以下操作:

val pm = context.packageManager
val hasFeatureLocation = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION)
var location: Location? = null
if (hasFeatureLocation) {
    val locationManager = context.getSystemService(LOCATION_SERVICE) as LocationManager
    val features = listof(
        PackageManager.FEATURE_LOCATION_GPS,PackageManager.FEATURE_LOCATION_NETWORK
    )
    for (f in features){
        val hasFeature = pm.hasSystemFeature(f)
        if (hasFeature) {
            //val isEnabled = locationManager.isProviderEnabled(f)
            location = locationManager.getLastKNownLocation(f)
        }
        if (location != null)
            break
    }
}

// this would work but needs google services
// val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
// location = fusedLocationClient.lastLocation.await<Location?>()

但这失败,出现java.lang.SecurityException: "android.hardware.location.gps" location provider requires ACCESS_FINE_LOCATION permission.

我想要什么:

  • 我只想使用ACCESS_COARSE_LOCATION(不能使用ACCESS_FINE_LOCATION
  • 我不想使用Google FusedLocationClient,因为我不想仅为此一项任务添加此依赖项
  • 我只需要大约。 GPS位置,正好适合ACCESS_COARSE_LOCATION权限

我只需要大约。向用户显示当地天气的位置。 Google FusedLocationClient可以在有限的ACCESS_COARSE_LOCATION权限下执行此操作,如果没有Google服务,是否可以这样做?有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)