我尝试按照here
所述访问片段中的最后一个已知位置在我的onCreate中:
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
在我的onResume:
@Override
public void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
Fragment实现了GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener
并且连接被称为!!
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
}
}
并且mLastLocation始终为null。每当我尝试调用GetLastLocation()时,它都会返回null。
知道为什么吗?
答案 0 :(得分:0)
我从未尝试过这种方法,我通常创建一个LocationProvider
来实现LocationsSource
和LocationListener
。以下是一个可以帮助您实现此方法的示例:
How to get My Location changed event with Google Maps android API v2?