为什么 FusedLocationApi.getLastLocation 为空 [英] Why is FusedLocationApi.getLastLocation null
问题描述
我正在尝试使用 FusedLocationApi.getLastLocation
获取位置,并且我在清单文件中获得了位置权限:
I am trying to get location by using FusedLocationApi.getLastLocation
and I've got the location permissions in the manifest file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
但是,当我从系统请求位置时,我得到了 null
.我只是在测试关闭和打开位置服务,所以它可能与此有关(当然,当我尝试这个时它是打开的).但即使在返回 null 之后,我也在等待 onLocationChanged
被调用,但它从未被调用过.我在这里也看到了一个类似的问题:FusedLocationApi.getLastLocation always null
However, I am getting null
when I request the location from the system. I was just testing turning off and on location services so it may be related to that (of course, it's ON when I'm trying this). But even after returning null, I'm waiting for onLocationChanged
to be called and it's never called. I've also seen a similar question here: FusedLocationApi.getLastLocation always null
这是我的代码:
protected LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(120000);
mLocationRequest.setFastestInterval(30000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
return mLocationRequest;
}
protected GoogleApiClient getLocationApiClient(){
return new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
LocationRequest locationRequest = createLocationRequest();
@Override
public void onLocationChanged(Location location) {
App.setLocation(location); // NEVER CALLED
}
@Override
public void onConnected(Bundle bundle) {
App.setLocation(LocationServices.FusedLocationApi.getLastLocation(locationApiClient)); //RETURNS NULL
LocationRequest locationRequest = createLocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(locationApiClient, locationRequest, this);
}
在我的应用程序的 onCreate
上:
And on onCreate
of my app:
locationApiClient = getLocationApiClient();
locationApiClient.connect();
其中 this
是我的应用程序对象.为什么我得到 null(是的,我确实知道在文档中所述的某些罕见情况下它可能为 null,但我总是得到这个,并非很少)和 此后仍未获得位置更新?如果有帮助,它是没有 SIM 卡的真实设备(三星 Galaxy S3).
Where this
is my application object.
Why am I getting null (yes, I do know it may be null in some rare circumstances as stated in the documents, but I'm ALWAYS getting this, not rarely) and still not getting location updates thereafter? It's a real device (Samsung Galaxy S3) without a SIM card, if it helps.
推荐答案
我在我的应用中使用 FusedLocationProvider api,这是我的代码,适用于设备和模拟器 -
I am using FusedLocationProvider api in my app, Here is my code that works both on device and emulator -
@Override
protected void onCreate(Bundle savedInstanceState){
//put your code here
....
getLocation();
}
private void getLocation(){
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(LOCATION_INTERVAL);
locationRequest.setFastestInterval(LOCATION_INTERVAL);
fusedLocationProviderApi = LocationServices.FusedLocationApi;
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
if (googleApiClient != null) {
googleApiClient.connect();
}
}
@Override
public void onConnected(Bundle arg0) {
fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(mContext, "location :"+location.getLatitude()+" , "+location.getLongitude(), Toast.LENGTH_SHORT).show();
}
这是我的工作代码.
希望我的代码对您有所帮助.
Hope my code help you.
干杯..
这篇关于为什么 FusedLocationApi.getLastLocation 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!