如何停止在循环器内部停用fusedLocationProviderClient

huangapple go评论81阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月13日 08:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定