GetLastLocation始终返回null [英] GetLastLocation always returns null
问题描述
我尝试访问片段中的最后一个已知位置,如此处
I try to access the last known location in a Fragment as explained here
在我的onCreate中:
in my onCreate:
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
在我的onResume中:
in my onResume:
@Override
public void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
Fragment实现了GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener
The Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
和Connected 被称为!!
and the on Connected is called!!
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
}
}
并且mLastLocation始终为空。每次我尝试调用GetLastLocation()时,都会返回null。
and the mLastLocation is always null. Everytime I try to call GetLastLocation() it returns null.
任何想法为什么?
推荐答案
我从来没有尝试过这种方法,我通常会创建一个 LocationProvider
实现 LocationsSource
和 LocationListener
。以下示例可以帮助您使用此方法:
如何使用Google Maps android API v2获取My Location改变的事件?
I never tried that approach, I usually create a LocationProvider
that implements LocationsSource
and LocationListener
. Here's an example that may help you with this approach:
How to get My Location changed event with Google Maps android API v2?
这篇关于GetLastLocation始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!