如何计算加速到100公里/小时的时间?当!location.hasSpeed()为真时,我注册了一个位置侦听器,将位置时间存储到一个变量中。当速度达到给定速度时,在这种情况下,100 of /h (27.77 m/s) i减去位置的speed,结果i除以1000。
这是“伪代码”
@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;
}
}
}
发布于 2011-09-28 16:29:07
我在这里看到的主要问题是,只要设备在移动,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时发生的事情。
https://stackoverflow.com/questions/7579165
复制相似问题