2016-04-29 63 views
-1
我有下面的代碼麻煩

啓動位置更新

protected void startLocationUpdates() { 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, LocationRequest mLocationRequest, this); 
    Location OldLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    String OldLocationTime = DateFormat.getTimeInstance().format(new Date()); 
} 

當我鍵入位置更新請求,位置請求進入了「無法解析符號mLocationRequest。」不過,我已經將它定義如下:

protected void createLocationRequest() { 
    LocationRequest mLocationRequest = new LocationRequest(); 
} 

而且OldLocation getLastLocation說調用需要的權限,這是我在的onCreate定義()

有誰知道什麼是錯在這裏嗎?

另外,我想知道如何每隔幾秒就得到一個位置對象的位置,並找出兩者之間的距離。很多人談論「回調對象」,但我並不完全理解這個概念。

任何人都可以幫助我嗎?

+0

嚴重? '(mGoogleApiClient,LocationRequest mLocationRequest,this)'? 'LocationRequest mLocationRequest'作爲參數?這是什麼意思?你知道java嗎? – Selvin

+1

你的'mLocationRequest'只在'createLocationRequest()'方法中有作用域。在方法之外聲明它並在方法內初始化它。 –

+0

@Selvin好吧,不多。這就是我問〜的原因。哦,我之所以這麼說是因爲Android Studio教程告訴我的。檢查:https://developer.android.com/training/location/receive-location-updates.html – ForceFieldsForDoors

回答

0

您以錯誤的方式傳遞參數。

改變這一行:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, LocationRequest mLocationRequest, this); 

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