-1

美好的一天。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裏面我要求位置更新,但沒有什麼只是簡單的工作,沒有我不需要使用任何類型的地圖來強制任何類型的模擬發生我只需要簡單地獲取用戶的經度和緯度,無論何時啓動應用程序,並且顯示的片段都是我想要的。 任何想法?

+0

可能的重複:http://stackoverflow.com/questions/29441384/fusedlocationapi-getlastlocation-always-null –

+0

你沒看過我的文章嗎?你沒有看到我說我不需要調用任何地圖更新位置! –

+0

這並沒有解決我的問題 –

回答

0

位置服務未立即就緒onConnected。嘗試爲postdelay操作添加處理程序。 onConnected後5秒獲取位置。

+0

不是一個解決方案,因爲回調沒有得到觸發,所以情況並非如此 –

0

getLastLocation文檔:

返回當前可用的最佳最近的位置。

如果某個位置不可用,而這種情況很少發生,則會返回空 。在尊重 位置權限的情況下,將返回最佳的準確性。

當最後一個位置不可用,你需要自己申請一個這樣的:

聲明一個實例變量:

private LocationRequest mLocationRequest; 

而且在onConnected做到這一點(不要忘記實現LocationListener):

@Override 
    public void onConnected(Bundle bundle) { 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
      // Create the LocationRequest object 
      mLocationRequest = LocationRequest.create() 
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
        .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
        .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } 
     else { 
      // Location is already available 
     } 
    } 

您會收到位於onLocationChanged回調:

@Override 
public void onLocationChanged(Location location) { 
    // Use the location 
} 

你應該叫removeLocationUpdates當你需要停止接收位置更新。

if (mGoogleApiClient.isConnected()) { 
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    mGoogleApiClient.disconnect(); 
} 

既然你需要的位置只有一次,當應用程序啓動時,您可以在onLocationChanged回調調用它。

+0

好吧我會嘗試這個,但在此之前,我有1個問題,我不需要任何寶貴的經度和緯度所以簡單的網絡提供將是完全完美的,因爲我需要寶貴的城市周圍,而不是街道或meteres,所以最大的甚至漂移不會給一個問題,基本上我說我看到你正在使用HIGH_ACCURACY,所以我可以使用POWER_LOW模式不是HIGH_ACCURACY,它會以同樣的方式工作?因爲高準確度將需要GPS,並且我不希望任何GPS被調用,因爲網絡是相當不夠的 –

+0

是的它應該以相同的方式工作,但準確度較低。 –

+0

完美,現在測試 –

相關問題