27

所以我正在处理 iPhone GPS 的一些准确性问题。我有一个使用位置的应用程序。

在委托方法locationManager: didUpdateToLocation: fromLocation:中,我返回了一个位置。从一些研究来看,GPS 在返回的第一个结果上似乎并不总是准确的,即使将desiredAccuracy属性设置为kCLLocationAccuracyBest.

为了解决这个问题,我不会stopUpdatingLocation在它newLocation:至少返回 3 次之前打电话(这非常快)。对于是否stopUpdatingLocation返回和返回newLocation. 我尝试的一种方法是检查纬度和经度并进行newLocation比较oldLocation,如果它们不相同,则保持位置更新运行。oldLocation我也试过检查和之间的距离newLocation,如果小于 20 米,没关系。这两个都经过至少 3 次运行的返回测试。后一种方式不那么“严格”,因为如果用户在移动的车辆中newLocationoldLocation很难做到 100% 相同。

现在,我的问题是,即使执行上述操作(基本上不接受一个位置,直到发生一些更新并CLLocationManager检查它们之间的距离CLLocations(或它们是否相同),我有时仍然会看到一些奇怪的位置结果,测试时。

如果我离开应用程序,进入 Maps.app,使用 GPS,然后打开多任务处理,强制退出我的应用程序,然后重新打开它以获得干净的启动,有时会得到修复。

人们用来解决同类问题的任何经验和可能的解决方案?评论和解决方案赞赏:)

4

2 回答 2

45

我不记得我从哪个项目获得了以下代码,但这对我来说非常有效(我记得它来自 WWDC 2010 视频)。在我的代码中,我保留了原始项目中的注释,希望这会有所帮助。

- (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.mApple 的“Locate Me”示例项目,可在:

https://developer.apple.com/library/content/samplecode/LocateMe/Introduction/Intro.html

于 2011-01-09T02:39:22.443 回答
0

从donkim的回答考虑更换这条线

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

带线

[NSObject cancelPreviousPerformRequestsWithTarget: self];
于 2012-07-02T14:05:26.023 回答