fusedLocationProviderClient.lastLocation.addOnSuccessListener始终为null [英] fusedLocationProviderClient.lastLocation.addOnSuccessListener always null

查看:123
本文介绍了fusedLocationProviderClient.lastLocation.addOnSuccessListener始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚更新了位置API以使用FusedLocationProviderClient,但是我遇到了这个问题,当我关闭GPS并打开GPS时,总是会得到空位置:

I just updated my Location API to use FusedLocationProviderClient but I am having this issue, when I turn off and on the GPS, I am always getting null location:

val fusedLocationProviderClient =
                        LocationServices.getFusedLocationProviderClient(callingActivity)
            fusedLocationProviderClient.flushLocations()
            getLocationRequest()

            checkLocationSettings(callingActivity, turnOnGpsRequestCode, callback) {
                // Location settings successful
                fusedLocationProviderClient.lastLocation
                        .addOnSuccessListener(callingActivity) {
                            location ->
                            // Here location is always null
callback.onCallback(MenumyRadar.RadarStatus.SUCCESS, location)
                        }
                        .addOnFailureListener {
                            callback.onCallback(MenumyRadar.RadarStatus.ERROR_UNKNOWN, null)
                        }
            }

直到我打开另一个使用位置信息的应用(如Google Maps或Uber),它才起作用.

It doesn´t work until I open another app which uses location, as Google Maps or Uber.

这个答案让我有些头绪 FusedLocationApi.getLastLocation始终为空

I have some clue thanks to this answer FusedLocationApi.getLastLocation always null

根据Google的解释:

And to Google´s explanation:

fusedLocationClient.lastLocation .addOnSuccessListener { location:位置? -> //得到了最后一个已知的位置.在极少数情况下,它可以为null. }

fusedLocationClient.lastLocation .addOnSuccessListener { location : Location? -> // Got last known location. In some rare situations this can be null. }

getLastLocation()方法返回一个Task,您可以使用该Task来获取具有纬度和经度坐标的Location对象 地理位置.位置对象在 以下情况:

The getLastLocation() method returns a Task that you can use to get a Location object with the latitude and longitude coordinates of a geographic location. The location object may be null in the following situations:

在设备设置中关闭了位置.结果可能是 即使先前已检索到最后一个位置,也为null,因为 禁用位置也会清除缓存.从未记录过的设备 它的位置,可能是新设备或 已恢复为出厂设置.上的Google Play服务 设备已重新启动,并且没有活动的融合位置提供程序 服务重启后请求位置的客户端.到 避免这种情况,您可以创建一个新客户并请求位置 更新自己.有关更多信息,请参见接收位置. 更新.

Location is turned off in the device settings. The result could be null even if the last location was previously retrieved because disabling location also clears the cache. The device never recorded its location, which could be the case of a new device or a device that has been restored to factory settings. Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested location after the services restarted. To avoid this situation you can create a new client and request location updates yourself. For more information, see Receiving Location Updates.

但是它没有说明如何处理,我该怎么办?

But it does not say how to handle this, what can I do?

推荐答案

我找到了一个解决方案,这就是发生的情况,当location为null时,表示位置缓存已清除,这是在关闭GPS时发生的,所以当我正在打开它,没有最后一个位置,我所做的就是:

I found a solution, this is what happens, when the location is null it means the location cache was cleared, this happens when turning off GPS, so when I was turning it on there was no last location to get, what I did was this:

checkLocationSettings(callingActivity, turnOnGpsRequestCode, callback) {
                // Location settings successful
                mFusedLocationProviderClient!!.lastLocation
                        .addOnSuccessListener(callingActivity) {
                            location ->
                            if (location == null || location.accuracy > 100) {
                                mLocationCallback = object : LocationCallback() {
                                    override fun onLocationResult(locationResult: LocationResult?) {
                                        stopLocationUpdates()
                                        if (locationResult != null && locationResult.locations.isNotEmpty()) {
                                            val newLocation = locationResult.locations[0]
                                            callback.onCallback(Status.SUCCESS, newLocation)
                                        } else {
                                            callback.onCallback(Status.ERROR_LOCATION, null)
                                        }
                                    }
                                }

                                mFusedLocationProviderClient!!.requestLocationUpdates(getLocationRequest(),
                                        mLocationCallback, null)
                            } else {
                                callback.onCallback(Status.SUCCESS, location)
                            }
                        }
                        .addOnFailureListener {
                            callback.onCallback(Status.ERROR_UNKNOWN, null)
                        }
            }

当位置为空时,使用回调开始请求位置,并且

When the location is null, start requesting locations using a callback and

mFusedLocationProviderClient !!.requestLocationUpdates(getLocationRequest(), mLocationCallback,空)

mFusedLocationProviderClient!!.requestLocationUpdates(getLocationRequest(), mLocationCallback, null)

然后,当调用回调时,将获得一个新位置,并再次开始获取位置.

Then when the callback is called, a new location is got and it starts getting location again.

有时候,当您打开GPS时,位置不是零,但是精度很差,所以我还要检查位置精度是否足够好(对我来说足够好是100米)

Sometimes it happens that when you turn on the GPS, the location is not null but the accuracy is bad, so I also check if location accuracy is good enough (For me good enough is 100 meters)

这篇关于fusedLocationProviderClient.lastLocation.addOnSuccessListener始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆