【发布时间】:2021-08-16 09:10:08
【问题描述】:
我正在使用网络提供商获取用户的最后一个已知位置。当我测试它时,它在我的一部手机上运行良好,但纬度和经度在另一部手机上返回空值。是否有替代位置管理器以获得更一致的结果。或者我的代码中是否还有其他错误
这是我的权限检查代码:
if (ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
AlertDialog.Builder(this)
.setCancelable(false)
.setTitle("Please Grant Location Permissions")
.setPositiveButton("Ok", null)
.show()
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
101
)
}
还有我的位置请求代码
longitude =
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)?.longitude
latitude =
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)?.latitude
val geocoder = Geocoder(this)
if (latitude != null && longitude != null) {
val addresses: List<Address> = geocoder.getFromLocation(latitude, longitude, 1)
val cityName = addresses[0].subAdminArea
}
【问题讨论】:
标签: android google-maps kotlin location geocoder