Fused Location Provider:有没有办法检查位置更新是否失败? [英] Fused Location Provider: Is there a way to check if a location update failed?

查看:598
本文介绍了Fused Location Provider:有没有办法检查位置更新是否失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我希望获得用户当前位置的一次性精确位置。

简介 当我使用 FusedLocationProviderApi.getLastLocation 时,有时候这会是空的或过时的位置,因为我发现这只是获取缓存的位置,并且不会请求新的位置更新。 p>

解决方案是只请求一次位置更新,如下所示。

  LocationRequest locationRequest = LocationRequest.create()
.setNumUpdates(1)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(0);

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this)

现在我一直获得更准确的位置。



我的问题



我可以确定位置更新是否失败?因为我只请求1次。



当获得位置更新时,调用此回调函数

  @Override 
public void onLocationChanged(Location location){

}

然而,如果位置更新失败,则不会被调用。



我试过的



我看到有一个ResultCallback。但是,即使一次性位置更新失败,onSuccess也总是被调用。

  LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this).setResultCallback(new ResultCallbacks< Status>(){
@Override
public void onSuccess(@NonNull Status status){
DebugUtils.log(requestLocationUpdates ResultCallback onSuccess ++ status.toString(),true,true);
}

@Override
public void onFailure(@NonNull Status status){
DebugUtils.log(requestLocationUpdates ResultCallback onFailure ++ status.toString(),true,true);
}
});

其他

使用 com.google.android.gms:play-services-location:8.4.0



感谢阅读,请帮助我。

解决方案

在挖掘了API多一点并做了一些测试之后,我找到了解决方案我的问题似乎正在工作。

  LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,new LocationCallback(){
@Override
public void onLocationResult(LocationResult result){
DebugUtils.log(onLocationResult);
}
$ b @Override
public void onLocationAvailability(LocationAvailability locationAvailability){
DebugUtils.log(onLocationAvailability:isLocationAvailable =+ locationAvailability.isLocationAvailable());
}
},null);

而不是使用 LocationListener 。我只使用 LocationCallback



onLocationResult 最新的位置结果基于 LocationRequest 。如果无法提供位置,则不会调用该位置,并且在我的测试期间,它不是位置被禁用并且GPS在室内使用等情况时不会被调用。请致电 result.getLastLocation()以获取实际的位置对象。

onLocationAvailablity 总是被调用。 locationAvailability.isLocationAvailable()如果设备位置已知且在活动LocationRequests所请求的提示内合理地更新,则返回true。如果无法确定位置可能来自许多原因,包括禁用位置设置或无法在设备环境中检索传感器数据,则返回False。

我已经在没有位置服务的情况下进行测试,仅在室内使用GPS。在每种情况下,我删除了所有现有的位置历史记录(以防止缓存的位置)。一个准确的最新位置总是返回。

其他人可以证明这一点吗?注意: requestLocationUpdates 调用的最后一个参数是活套。传入null意味着它将使用调用线程的消息队列。

Introduction

In my app I want to get a one-off accurate location of where the user currently is. When I used FusedLocationProviderApi.getLastLocation sometimes this would be null or out of date location because I found out this just gets a cached location and does not request a new location update.

The solution was to request a location update only once as seen below.

 LocationRequest locationRequest  = LocationRequest.create()
                .setNumUpdates(1)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(0);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this)

Now I get a more accurate location all the time.

My Question

How can I determine if a location update failed? Since I am only requesting 1.

When a location update is obtained this callback is invoked

 @Override
 public void onLocationChanged(Location location) {

 }

However this does not get called if a location update failed.

What I have tried

I saw there was a ResultCallback. However, onSuccess seems to be always called even if the one-off location update failed.

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this).setResultCallback(new ResultCallbacks<Status>() {
            @Override
            public void onSuccess(@NonNull Status status) {
                DebugUtils.log("requestLocationUpdates ResultCallback onSuccess + " + status.toString(),true,true);
            }

            @Override
            public void onFailure(@NonNull Status status) {
                DebugUtils.log("requestLocationUpdates ResultCallback onFailure + " + status.toString(),true,true);
            }
        });

Other

Using com.google.android.gms:play-services-location:8.4.0

Thanks for reading, please help me out.

解决方案

After digging around the API a little more and doing some tests, I found a solution to my problem which seems to be working.

 LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult result) {
                DebugUtils.log("onLocationResult");
            }

            @Override
            public void onLocationAvailability(LocationAvailability locationAvailability) {
                DebugUtils.log("onLocationAvailability: isLocationAvailable =  " + locationAvailability.isLocationAvailable());
            }
        }, null);

Instead of using LocationListener. I used LocationCallback

onLocationResult is only called to provide the latest Location result based on the LocationRequest. It is not called if a location could not be provided and during my tests it is not when Location was disabled and GPS was used indoors etc. Please call result.getLastLocation() to get the actual location object.

onLocationAvailablity is always called. locationAvailability.isLocationAvailable() Returns true if the device location is known and reasonably up to date within the hints requested by the active LocationRequests. False is returned when failure to determine location may from a number of causes including disabled location settings or an inability to retrieve sensor data in the device's environment.

I have tested with no location services, GPS only while indoors. In each case I removed all existing location history (to prevent cached locations). An accurate up-to-date location was always returned.

Could others vouch for this?

Note: The last parameter for the requestLocationUpdates call is a looper. Passing in null means it will use the calling thread's message queue.

这篇关于Fused Location Provider:有没有办法检查位置更新是否失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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