2011-09-28 97 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