2016-06-14 11 views
0

我從googleApiClient獲取位置,當我在google的alert提示生成器中單擊yes時,則使用onActvityResult檢查用戶是否單擊是,然後設置我的文本查看該位置,我沒有在同一時間得到最後的位置,所以我用while while循環,這是一個很好的做法嗎? 這些是獲取位置的函數,我在我的活動中將它設置爲onActivityResult方法中的文本視圖,請告訴我,使用這個方法雖然是一個好的做法?使用do while循環從google api獲取位置

public Location getCurrentLatLong() { 
    checkLastLocation(); 
    Log.w("checkingsAct", mLastLocation + ""); 
    if(mLastLocation != null) { 
     Log.w("checkingsAct", mLastLocation+""); 
     latitude = mLastLocation.getLatitude(); 
     longitude = mLastLocation.getLongitude(); 
     mLastLocation.setLatitude(latitude); 
     mLastLocation.setLongitude(longitude); 
     Log.w("discheck", latitude + "" + " " + longitude); 
    } 
    return mLastLocation; 

} 
public void checkLastLocation() { 
    do { 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     Log.w("checkingsAct", mLastLocation + "do called"); 
    } while(mLastLocation == null); 

回答

0

您需要請求位置更新像下面

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, locationListener); 

如果一旦locaiton更新後,您會得到回電void onLocationChanged(Location location)方法。

+0

和TextView的 –

+0

然後在這個位置改變方法,我可以用設置文本是的,你可以用這個方法來設置TextView的 – Chandrakanth

+0

OK,日Thnx,我試圖 –