Android FusedLocationApi.getLastLocation和RequestLocationUpdate需要很长时间才能返回

Android FusedLocationApi.getLastLocation和RequestLocationUpdate需要很长时间才能返回,android,geolocation,google-play-services,fusedlocationproviderapi,android-fusedlocation,Android,Geolocation,Google Play Services,Fusedlocationproviderapi,Android Fusedlocation,我今天面临一个新手机的问题。当我这样做时: mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(150000); mLocationRequest.setFastestInterval(30000); mLocationRequest.setMaxWaitTime(900000); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_

我今天面临一个新手机的问题。当我这样做时:

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(150000);
mLocationRequest.setFastestInterval(30000);
mLocationRequest.setMaxWaitTime(900000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(25);

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
if (location == null) { Log.w(TAG, "getLastLocation return null"); }
else { onLocationChanged(location); }

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

问题是
LocationServices.FusedLocationApi.getLastLocation
返回null。通常这不是什么大问题,因为我们还设置了
requestLocationUpdates
。但问题是RequestLocationUpdate需要很长时间才能返回。我怎么说才能让
requestLocationUpdates
以尽可能快的速度返回第一个位置,并像我们配置的
mLocationRequest
那样间隔返回以下位置?

您应该创建两个不同的位置请求,一个用于定期位置更新,为了快速获取当前位置,还有这样一种方法:

mLocationRequest2 = new LocationRequest();
mLocationRequest2.setInterval(500);
mLocationRequest2.setFastestInterval(0);
mLocationRequest2.setMaxWaitTime(1000);
mLocationRequest2.setNumUpdates(1);
mLocationRequest2.setPriority(LocationRequest.HIGH_ACCURACY);

setNumUpdates
将确保此LocationRequest只被调用一次。

您应该创建两个不同的位置请求,一个用于定期位置更新,另一个类似于此的用于快速获取当前位置:

mLocationRequest2 = new LocationRequest();
mLocationRequest2.setInterval(500);
mLocationRequest2.setFastestInterval(0);
mLocationRequest2.setMaxWaitTime(1000);
mLocationRequest2.setNumUpdates(1);
mLocationRequest2.setPriority(LocationRequest.HIGH_ACCURACY);

setNumUpdates
将确保此LocationRequest只被调用一次。

因此我可以同时执行两个请求,如LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleapClient,mlLocationRequest,this);紧接着是LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleapClient,mlLocationRequest2,this)?嗯。。我需要重新阅读文件,但我认为你不能。。我认为你需要在适合你的目的时在它们之间切换。分离只是为了方便。因此,我可以同时执行两个请求,如LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleapClient,mlLocationRequest,this);紧接着是LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleapClient,mlLocationRequest2,this)?嗯。。我需要重新阅读文件,但我认为你不能。。我认为你需要在适合你的目的时在它们之间切换。分离只是为了方便。