Android 在一定时间内获得最佳可能位置

Android 在一定时间内获得最佳可能位置,android,geolocation,google-play-services,android-location,Android,Geolocation,Google Play Services,Android Location,在我的应用程序中,我需要获得用户的位置(一次,而不是连续跟踪),以计算路线。因此,位置需要尽可能准确,但我不想让用户在我的应用程序尝试获取准确位置时等待太久 Im使用Google Play services位置api,如下所示: LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locati

在我的应用程序中,我需要获得用户的位置(一次,而不是连续跟踪),以计算路线。因此,位置需要尽可能准确,但我不想让用户在我的应用程序尝试获取准确位置时等待太久

Im使用Google Play services位置api,如下所示:

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setNumUpdates(1); 
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest, this);
mLocationHandler.sendEmptyMessageDelayed(0, 15000); // Time out location request
final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(16)         
        .setFastestInterval(16)    // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if(mGoogleApiClient.isConnected()) {            
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);             
}
我有一个处理程序mLocationHandler,它在15秒后取消locationRequest,因为我不想让用户等待太久


有没有办法,如果高精度定位超时,我如何尝试获取不太准确的位置?

您可以在超时后尝试

Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
它将为您提供最后已知的位置,并检查其准确性是否优于您从位置更新中获得的结果

更新您的更新,如下所示:

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setNumUpdates(1); 
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest, this);
mLocationHandler.sendEmptyMessageDelayed(0, 15000); // Time out location request
final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(16)         
        .setFastestInterval(16)    // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if(mGoogleApiClient.isConnected()) {            
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);             
}
这将确保您有更新。然后,您可以减少超时时间,应该不超过一秒钟。然后,您可以删除locationupdaterequest,如下所示:

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setNumUpdates(1); 
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest, this);
mLocationHandler.sendEmptyMessageDelayed(0, 15000); // Time out location request
final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(16)         
        .setFastestInterval(16)    // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if(mGoogleApiClient.isConnected()) {            
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);             
}

我为此使用外部库:。检查
冷却器示例
部分,该部分准确显示了您想要的:

LocationRequest req = LocationRequest.create()
                         .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                         .setExpirationDuration(TimeUnit.SECONDS.toMillis(LOCATION_TIMEOUT_IN_SECONDS))
                         .setInterval(LOCATION_UPDATE_INTERVAL);

Observable<Location> goodEnoughQuicklyOrNothingObservable = locationProvider.getUpdatedLocation(req)
            .filter(new Func1<Location, Boolean>() {
                @Override
                public Boolean call(Location location) {
                    return location.getAccuracy() < SUFFICIENT_ACCURACY;
                }
            })
            .timeout(LOCATION_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS, Observable.just((Location) null), AndroidSchedulers.mainThread())
            .first()
            .observeOn(AndroidSchedulers.mainThread());

goodEnoughQuicklyOrNothingObservable.subscribe(...);
LocationRequest req=LocationRequest.create()
.setPriority(定位请求。优先级高精度)
.setExpirationDuration(时间单位.SECONDS.toMillis(位置超时单位:秒))
.setInterval(位置更新间隔);
Observable goodEnoughQuicklyOrNothingObservable=locationProvider.getUpdatedLocation(req)
.filter(新函数1(){
@凌驾
公共布尔调用(位置){
返回位置。getAccurance()<足够的精度;
}
})
.timeout(LOCATION\u timeout\u以秒为单位,TimeUnit.SECONDS,Observable.just((LOCATION)null),AndroidSchedulers.mainThread()
.first()
.observeOn(AndroidSchedulers.mainThread());
好的,足够快的或不可观测的。订阅(…);

在位置更新中,您可能希望在间隔(延迟)内请求尽可能多的位置,然后选择最佳位置。然后,问题是在某些情况下,我在间隔(15秒)内没有收到任何位置更新。也许我需要设置优先级其他一些高精度的东西?更改您的位置更新。检查我的更新答案。通常情况下,位置会在几秒钟内返回,但从分析中,我可以看到我的超时有时会被击中。我不知道用户是在大建筑物内还是什么,但无论如何应用程序都无法获取位置。在您更新的答案中,与我最初的位置请求(已删除的SetNumUpdate)没有太大区别。在我无法在原始请求中获得任何位置更新的情况下,这是否会造成任何差异?如果服务可用,则将强制它在第一次更新后每16毫秒进行一次更新。但永远不会超过几秒钟。。。