0

時速100kmまでの加速時間はどのように計算できますか? さて、!location.hasSpeed() が true の場合に位置リスナーを登録し、位置の時間を変数に格納しました。この場合、速度が 100km/h (27.77 m/s) の速度に達すると、場所の速度から減算し、結果を 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;
            }
        }
    }
4

1 に答える 1

0

ここで見られる主な問題は、デバイスが移動するたびstartAccelerationToTimeにリセットされることです。(1 つ目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のときに発生するものをトリガーするために、更新をリクエストする(おそらく「リセット」ボタンなどを介して)か、特定のパラメーターを設定する必要がある場合があります。

于 2011-09-28T08:29:06.810 に答える