问题描述
我正在使用FusedLocationProviderClient
访问用户的lastLocation
。如果为空,我正在等待requestLocationUpdates
返回。
我已经通过重新启动设备在Pixel 3a上对其进行了测试,该操作会清除上一个位置,但是返回位置需要2分钟以上的时间。
这是我的代码。我在做什么错了?
private fun onLocationPermissionGranted() {
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(
requireContext()
)
fusedLocationProviderClient.flushLocations()
Log.i("Jamie","Attempting to access last location")
val locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
locationRequest.interval = 5000
locationRequest.numUpdates = 1
val callback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
Log.i("Jamie","Calling location permission granted again")
onLocationPermissionGranted()
}
}
fusedLocationProviderClient.lastLocation
.addOnSuccessListener { location: Location? ->
Log.i("Jamie","FusedLocation onsuccess called")
if (location == null) {
Log.i("Jamie","location null")
}
location?.let {
fusedLocationProviderClient.removeLocationUpdates(callback)
Log.i("Jamie","Location received")
this.map.isMyLocationEnabled = true
viewmodel.currentLocation = location
updateCamera(location.latitude,location.longitude)
}
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest,callback,Looper.getMainLooper())
}
2020-08-31 14:04:33.834 3780-3780/com.redacted I/Jamie: Attempting to access last location
2020-08-31 14:04:34.547 3780-3780/com.redacted I/Jamie: FusedLocation onsuccess called
2020-08-31 14:04:34.547 3780-3780/com.redacted I/Jamie: location null
2020-08-31 14:06:50.404 3780-3780/com.redacted I/Jamie: Calling location permission granted again
2020-08-31 14:06:50.404 3780-3780/com.redacted I/Jamie: Attempting to access last location
2020-08-31 14:06:50.517 3780-3780/com.redacted I/Jamie: FusedLocation onsuccess called
2020-08-31 14:06:50.517 3780-3780/com.redacted I/Jamie: Location received
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)