如何根据请求实现位置更新,这样我就可以不断获取用户的当前位置,而不是获取用户的最后位置。 代码是:https://pastebin.com/8vLb0NF6 这就是我现在获取用户位置的方法:
private fun getCurrentLocation() {
if(checkPermissions()){
if(isLocationEnabled()){
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions()
return
}
fusedLocationProviderClient.lastLocation.addOnCompleteListener(this){
task -> val location: Location?=task.result
if(location == null){
Toast.makeText(this, "Unknown exception", Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(this, "Great Success", Toast.LENGTH_SHORT).show()
}
}
}
else{
Toast.makeText(this, "Turn on your location", Toast.LENGTH_SHORT).show()
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
}
}
else{
requestPermissions()
}
}
我的问题中的简短演示程序在这里
Android位置提供了未来的时间戳,还是我很蠢?亲自尝试一下
提供可靠的方法来获取定期位置更新。您可以在演示中在一处将 GPS_PROVIDER 更改为 FUSED_PROVIDER。
我不知道您的应用程序是什么,但请注意,位置修复可能会受到“抖动”(跳动几米)和有时阶跃函数变化(例如,当“最佳三个”卫星发生变化时)的影响。当我在同一次步行中并排比较不同手机时,它们的表现截然不同!
还请注意,通常前几个位置比后面的位置跳跃得更多。
我希望这有帮助。