ios - CLLocationManager 和准确性问题 - 有什么经验吗?

标签 ios cllocationmanager cllocation

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

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

为了解决这个问题,我不会在返回 newLocation: 之前调用 stopUpdatingLocation 至少 3 次(这非常快)。我还研究了另外两个关于是否 stopUpdatingLocation 和返回 newLocation 的“要求”。我尝试的一种方法是检查 newLocation 的纬度和经度,并与 oldLocation 进行比较,如果它们不相同,则保持位置更新运行。我还尝试检查 oldLocationnewLocation 之间的距离,如果小于 20 米,就可以了。这两个都经过至少 3 次运行的返回测试。后一种方法不太“严格”,因为如果用户在移动的车辆中,newLocationoldLocation 很难做到 100% 相同。

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

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

人们有什么经验和可能的解决方案来解决同类问题?评论和解决方案表示赞赏:)

最佳答案

我不记得我从哪个项目中获得了以下代码,但这对我来说非常有效(我记得它来自 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];
        }
    }
}

希望这对您有所帮助!

通过 Apple 的“Locate Me”示例项目中的 GetLocationViewController.m,网址为:

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

关于ios - CLLocationManager 和准确性问题 - 有什么经验吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4635938/

相关文章:

ios - 从数组创建字典

iphone - 使用 CLLocationManager 获取准确位置

ios - 计算 Swift 中两个 CLLocation 点之间的方位

ios - 不时更新位置

ios - 在任何时候在iOS 7或更高版本中在后台启动定位服务

iphone - IOS位置发现检测

ios - Swift didReceiveRemoteNotification 在应用程序中触发

iphone - 如何在 iphone 中以编程方式创建 PLIST 文件

ios - 错误 : 'Unable to add mapping for keyPath news, one already exists...'

ios - CLLocation 为不同的 iPhone 设备返回不同的 CLLocationSpeed 值