0
我有這樣的代碼:onLocationChange()沒有被調用?計算距離走遍-Android
Location newLocation = new Location(LocationManager.GPS_PROVIDER);
Location oldLocation = new Location(LocationManager.GPS_PROVIDER);
float distanceTravelled=0;
onLocationChanged:
@Override
public void onLocationChanged(Location location)
{
oldLocation.set(newLocation);
startactivity();
distanceTravelled+=newLocation.distanceTo(oldLocation);
String stringDistance= Float.toString(distanceTravelled);
TextView distance = (TextView) findViewById(R.id.textDistance);
distance.setText(stringDistance);
}
startactivity:
public void startactivity()
{
GPSTracker gps = new GPSTracker(Live.this);
if(gps.canGetLocation())
{
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is: \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
newLocation.setLatitude(latitude);
newLocation.setLongitude(longitude);
}
else
{
gps.showSettingsAlert();
}
}
GPSTracker是一個單獨的類,它獲取當前的緯度和經度(它工作正常!)。
GPS更新至少。距離5米和分鐘。時間5秒。
這裏是我的全部GPSTracker類:GPSTracker.java
我已經在清單中添加三個權限:粗調,微調和互聯網。
我的問題是onLocationChanged裏面的textview永遠不會被改變。
什麼問題? onLocationChanged沒有被調用,或者有一些邏輯錯誤(距離始終爲零)?
如何解決這個問題?
似乎是正確的邏輯,你認爲這是否行得通呢?我現在無法測試它,只要位置發生變化,就會更新textView。 我是否也需要調用startactivity? –
我不認爲你需要在'onLocationChanged'中調用'startactivity()',一旦開始活動就足夠了。 – Dahaka