美好的一天。Google Api客戶端位置更新無法正常工作,第二次嘗試後lastKnownLocation爲NULL
我需要在應用程序啓動時獲取位置。 位置聚集發生在片段內部,就像這樣。
我正在裝修這樣的客戶端。
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
// The next two lines tell the new client that 「this」 current class will handle connection stuff
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
//fourth line adds the LocationServices API endpoint from GooglePlayServices
.addApi(LocationServices.API)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_LOW_POWER);
@Override
public void onStart() {
super.onStart();
if (mSharedHelper.showGreeting()) {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
onConnected
回調被觸發,我正在做下一個。
@Override
public void onConnected(Bundle bundle) {
try {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
failedLocate = true;
hideGreetingCard();
} else {
failedLocate = false;
//If everything went fine lets get latitude and longitude
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
YahooWeather yahooWeather = YahooWeather.getInstance();
yahooWeather.queryYahooWeatherByLatLon(getActivity(), currentLatitude, currentLongitude, this);
}
} catch (SecurityException e) {
e.printStackTrace();
}
}
的問題是位置變量總是空不管我已經試過什麼優先無論我沒做什麼工作,我把所有的權限,增加內部清單,並在運行時的相應要求,並且除了位置變量爲空,onLocationChanged
回調永遠不會被觸發,但你可以看到onConnected
裏面我要求位置更新,但沒有什麼只是簡單的工作,沒有我不需要使用任何類型的地圖來強制任何類型的模擬發生我只需要簡單地獲取用戶的經度和緯度,無論何時啓動應用程序,並且顯示的片段都是我想要的。 任何想法?
可能的重複:http://stackoverflow.com/questions/29441384/fusedlocationapi-getlastlocation-always-null –
你沒看過我的文章嗎?你沒有看到我說我不需要調用任何地圖更新位置! –
這並沒有解決我的問題 –