2011-09-28 48 views
0

如何計算加速到100kmh的時間? 那麼,當!location.hasSpeed()爲true時,我註冊了一個位置監聽器,將位置的時間存儲到一個變量中。當速度是給定速度的範圍在此情況下100公里每小時(27.77米/ s)的我從位置的spped和我通過1000Android的java開發時間加速到

這裏劃分結果。減去是「僞碼」

@Override 
    public void onLocationChanged(Location currentLoc) { 

     // when stop reseted, when start reset again 
     if (isAccelerationLoggingStarted) { 
      if (currentLoc.hasSpeed() && currentLoc.getSpeed() > 0.0) { 
       // dismiss the time between reset to start to move 
       startAccelerationToTime = (double) currentLoc.getTime(); 
      } 
     } 

     if (!currentLoc.hasSpeed()) { 
      isAccelerationLoggingStarted = true; 
      startAccelerationToTime = (double) currentLoc.getTime(); 
      acceleration100 = 0.0; 
     } 

     if (isAccelerationLoggingStarted) { 
      if (currentLoc.getSpeed() >= 27.77) { 
       acceleration100 = (currentLoc.getTime() - startAccelerationToTime)/1000; 
       isAccelerationLoggingStarted = false; 
      } 
     } 
    } 

回答

0

我在這裏看到的主要問題是,每當設備在移動,startAccelerationToTime被重置。 (第一個if只檢查是否有移動;它不檢查是否已經有記錄的開始時間

我看不到需要在哪裏isAccelerationLoggingStarted - 速度和變量本身可以是。清理了一下,以明確下一步應該是什麼

你大概僞代碼應該看起來像:

if speed is 0 
    clear start time 
else if no start time yet 
    start time = current time 
    clear acceleration time 
else if no acceleration time yet, and if speed >= 100 mph 
    acceleration time = current time - start time 

在Java中,會看起來像......

long startTime = 0; 
double accelerationTime = 0.0; 

@Override 
public void onLocationChanged(Location currentLoc) { 

    // when stopped (or so slow we might as well be), reset start time 
    if (!currentLoc.hasSpeed() || currentLoc.getSpeed() < 0.005) { 
     startTime = 0; 
    } 

    // We're moving, but is there a start time yet? 
    // if not, set it and clear the acceleration time 
    else if (startTime == 0) { 
     startTime = currentLoc.getTime(); 
     accelerationTime = 0.0; 
    } 

    // There's a start time, but are we going over 100 km/h? 
    // if so, and we don't have an acceleration time yet, set it 
    else if (accelerationTime == 0.0 && currentLoc.getSpeed() >= 27.77) { 
     accelerationTime = (double)(currentLoc.getTime() - startTime)/1000.0; 
    } 
} 

現在,我不確定位置監聽者的工作方式,或者他們在移動時通知您的頻率。所以這可能只是半工半工。特別是,當您不移動時,onLocationChanged可能不會被調用;您可能需要請求更新(也許通過「重置」按鈕或某物)或設置某些參數以觸發速度== 0時發生的情況。

+0

謝謝。但是出現了另一個問題。 從開始移動到GPS所花費的時間實現了移動並觸發onLocationChange事件。 – Laszlo

+0

我假設部分取決於設備,部分取決於您傳遞給'requestLocationUpdates'的條件。 – cHao

+0

我也將零傳遞給minTime和minDistance – Laszlo