GetLastLocation始终返回null

时间:2015-06-01 21:00:22

标签: android location google-play-services google-api-client

我尝试按照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。

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

我从未尝试过这种方法,我通常创建一个LocationProvider来实现LocationsSourceLocationListener。以下是一个可以帮助您实现此方法的示例: How to get My Location changed event with Google Maps android API v2?