1

場所に関するAndroid開発者のブログ投稿を読んでいるときに、次のコードに出くわしました(ブログから切り取って貼り付けます)。

List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
  Location location = locationManager.getLastKnownLocation(provider);
  if (location != null) {
    float accuracy = location.getAccuracy();
    long time = location.getTime();

    if ((time > minTime && accuracy < bestAccuracy)) {
      bestResult = location;
      bestAccuracy = accuracy;
      bestTime = time;
    }
    else if (time < minTime && 
         bestAccuracy == Float.MAX_VALUE && time > bestTime){
      bestResult = location;
      bestTime = time;
    }
  }
}

残りはかなり明確ですが、この行は私を困惑させます:

    else if (time < minTime && 
         bestAccuracy == Float.MAX_VALUE && time > bestTime){

「時間」は、許容可能なレイテンシ期間内であり、以前のbestTimeよりも新しい必要があります。それは理にかなっている。

しかし、bestAccuracyとMax Floatの値の比較はどういう意味ですか?精度がフロートが保持できる最大値と正確に等しくなるのはいつですか?

4

2 に答える 2

2

ソースファイル全体への彼のリンクをたどると、その特定のビットはより理にかなっています。少し大きいスニペットを次に示します。

Location bestResult = null;
float bestAccuracy = Float.MAX_VALUE;
long bestTime = Long.MIN_VALUE;

// Iterate through all the providers on the system, keeping
// note of the most accurate result within the acceptable time limit.
// If no result is found within maxTime, return the newest Location.
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
  Location location = locationManager.getLastKnownLocation(provider);
  if (location != null) {
    float accuracy = location.getAccuracy();
    long time = location.getTime();

    if ((time > minTime && accuracy < bestAccuracy)) {
      bestResult = location;
      bestAccuracy = accuracy;
      bestTime = time;
    }
    else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) {
      bestResult = location;
      bestTime = time;
    }
  }
}

非常に簡単に言えば、Float.MAX_VALUE彼のデフォルト値はであり、彼は前の節bestAccuracyでそれを減らしていないことを確認しているだけです。if

于 2011-07-01T17:21:23.000 に答える
2

Float.MAX_VALUEbestAccuracyに初期化されていると思います。その場合、コードは次のように要約できるように見えます。minTimeよりも長い時間で最小(最高?)の精度を持つプロバイダーを見つけます。minTimeを超える時間がない場合は、minTimeに最も近い時間を取得します。

これはからリファクタリングできます

    if ((time < minTime && accuracy < bestAccuracy)) {
      bestResult = location;
      bestAccuracy = accuracy;
      bestTime = time;
    }
    else if (time > minTime && bestAccuracy == Float.MAX_VALUE && time < bestTime) {
      bestResult = location;
      bestTime = time;
    }

    if ((time < minTime && accuracy < bestAccuracy)) {
      bestResult = location;
      bestAccuracy = accuracy;
      bestTime = time;
      foundWithinTimeLimit = true;
    }
    else if (time > minTime && !foundWithinTimeLimit && time < bestTime) {
      bestResult = location;
      bestTime = time;
    }

少し明確になります。

于 2011-07-01T17:23:47.543 に答える