英文:
How to stop fusedLocationProviderClient inside the looper
问题
我对Kotlin有一些了解,所以这可能是一个非常简单的修复。基本上,我按照教程使用fusedLocationProviderClient获取GPS位置,现在我想要在找到位置后停止 looper(即,我希望它找到位置然后停止)。我对loopers不太熟悉,但我该如何创建一个与fusedLocationProviderClient一起工作并且可以在找到位置后退出的looper呢?
以下是代码:
private fun setUpLocationListener() {
checkSmsPermission(PERMISSION_FINE_LOCATION)
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
// 用于以高精度在每2秒后获取当前位置更新
val locationRequest = LocationRequest().setInterval(2000).setFastestInterval(2000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
fusedLocationProviderClient.requestLocationUpdates(
locationRequest,
object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
Log.d(TAG, "Location found")
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
Log.d(TAG, location.latitude.toString())
Log.d(TAG, location.longitude.toString())
}
if (locationResult.locations.size > 0) {
// 在这里退出
}
}
},
Looper.myLooper()!!
)
}
我认为主要需要帮助的地方是如何在fusedLocationProviderClient中使用loopers来创建一个可以在内部退出的looper。
提前感谢您的帮助。
英文:
I am slightly new to kotlin, so this may be a super easy fix. Basically, I followed a tutorial to get gps locations using the fusedLocationProviderClient and I am trying to stop the looper once it has found a location (i.e. I want it to find a location and then stop). I'm not very familiar with loopers, but how do I create a looper that works with the fusedLocationProviderClient that I can quit after I find a location.
Here is the code:
private fun setUpLocationListener() {
checkSmsPermission(PERMISSION_FINE_LOCATION)
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
// for getting the current location update after every 2 seconds with high accuracy
val locationRequest = LocationRequest().setInterval(2000).setFastestInterval(2000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
fusedLocationProviderClient.requestLocationUpdates(
locationRequest,
object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
Log.d(TAG, "Location found")
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
Log.d(TAG, location.latitude.toString())
Log.d(TAG,location.longitude.toString())
}
if (locationResult.locations.size > 0){
// I want to quit here
}
}
},
Looper.myLooper()!!
)
}
Mostly I think I need a bit of help on how to use loopers with fusedLocationProviderClient to create a looper that I can quit from inside.
Thanks in advance for your help
答案1
得分: 1
你可以从fusedClient
中调用removeLocationUpdates
来停止接收更新。
override fun onLocationResult(locationResult: LocationResult) {
Log.d(TAG, "找到位置")
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
Log.d(TAG, location.latitude.toString())
Log.d(TAG, location.longitude.toString())
}
if (locationResult.locations.size > 0){
fusedLocationProviderClient.removeLocationUpdates(this)
}
}
英文:
You could removeLocationUpdates
from fusedClient
to stop receiving updates
override fun onLocationResult(locationResult: LocationResult) {
Log.d(TAG, "Location found")
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
Log.d(TAG, location.latitude.toString())
Log.d(TAG,location.longitude.toString())
}
if (locationResult.locations.size > 0){
fusedLocationProviderClient.removeLocationUpdates(this)
}
}
答案2
得分: 0
以下是翻译好的部分:
对于可能有与我相同问题的人,这是我所做的:
对于位置请求变量,我只是移除了".setInterval(2000).setFastestInterval(2000)"。显然,启用了这些选项后,GPS位置总是会被重新定位。
英文:
For those who may have had the same questions as me, here's what I did:
for the location request variable, I just removed the ".setInterval(2000).setFastestInterval(2000)". Obviously, with these enabled, the GPS location always was going to get refound.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论