質問

ですから、iPhone GPSのいくつかの正確な問題を扱っています。場所を使用するアプリがあります。

デリゲートメソッドで locationManager: didUpdateToLocation: fromLocation: 私は場所に返されています。ちょっとした調査から、GPSは、設定した場合でも最初に戻る結果で必ずしも正確ではないようです desiredAccuracy プロパティへ kCLLocationAccuracyBest.

これを回避するために、私は電話しません stopUpdatingLocation 返される前に newLocation: 少なくとも3回(これは非常に速い)。私はまた、するかどうかについて、他の2つの「要件」で遊んでいます stopUpdatingLocation そして、戻ります newLocation. 。私が試した1つの方法は、緯度をチェックして長いことでした newLocation と比較してください oldLocation これらが同一でない場合は、場所を実行し続けてください。また、間の距離を確認してみました oldLocationnewLocation そして、20メートル未満の場合は大丈夫です。これらは両方とも、少なくとも3回の実行を返すことでテストされます。後者の方法は「厳格」ではありません。 newLocationoldLocation ユーザーが動いている車両にいる場合、100%同一であることで逃げるのは非常に難しいです。

さて、私の問題は、上記を行うときでさえ(基本的に場所を受け入れていない場合、いくつかの更新が発生するまで CLLocationManager 間の距離を確認します CLLocations (またはそれらが同一かどうか)私は、テスト時に時々場所についてやや奇妙な結果をまだ見ています。

アプリを離れ、maps.appに移動し、GPSを使用してからマルチタスクを開いて、フォースをquit quit quit quit quit and repen ent neopen fore clean起動すると修正される場合があります。

人々が同じ種類の問題を回避するために使用した経験や可能な解決策はありますか?コメントとソリューションが高く評価されています:)

役に立ちましたか?

解決

どのプロジェクトが次のコードを入手したのかを覚えていませんが、これは私にとって本当にうまくいきました(WWDC 2010のビデオからだったことを覚えています)。私のコードでは、元のプロジェクトのコメントをTACTで残したので、これが役立つことを願っています。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];

    if (locationAge > 5.0) return;

    // test that the horizontal accuracy does not indicate an invalid measurement
    if (newLocation.horizontalAccuracy < 0) return;

    // test the measurement to see if it is more accurate than the previous measurement
    if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
        // store the location as the "best effort"
        self.bestEffortAtLocation = newLocation;

        // test the measurement to see if it meets the desired accuracy
        //
        // IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue 
        // accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of 
        // acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout.
        //
        if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
            // we have a measurement that meets our requirements, so we can stop updating the location
            // 
            // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
            //
            [self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")];

            // we can also cancel our previous performSelector:withObject:afterDelay: - it's no longer necessary
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:nil];
        }
    }
}

お役に立てれば!

経由 GetLocationViewController.m Appleの「Locate Me」サンプルプロジェクトでは、

https://developer.apple.com/library/content/samplecode/locateme/introduction/intro.html

他のヒント

ドンキムの回答から、この行を交換することを検討してください

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:nil];

ライン付き

[NSObject cancelPreviousPerformRequestsWithTarget: self];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top