【问题标题】:CLLocationManager and accuracy issues - any experiences?CLLocationManager 和准确性问题 - 有什么经验吗?
【发布时间】:2011-06-05 21:01:32
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: ios cllocationmanager cllocation


    【解决方案1】:

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

    - (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

    【讨论】:

    • 啊,完美!我应该更仔细地查看返回的 CLLocation newLocation 的属性,我没有意识到它也有一个时间戳。我可能会将这个与我自己的代码一起使用。非常感谢!
    • 这正是我想要的。谢谢。
    • kCLLocationAccuracyBest 还是 kCLLocationAccuracyBestNavigation 更准确?我也需要速度。
    • locationManager.desiredAccuracy 在我的代码中始终为-1,怎么会?顺便说一句,它不应该是 manager.desiredAccuracy 吗?
    【解决方案2】:

    根据 donkim 的回答,考虑替换这一行

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

    带线

    [NSObject cancelPreviousPerformRequestsWithTarget: self];
    

    【讨论】:

      猜你喜欢
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2020-04-04
      • 2022-01-05
      相关资源
      最近更新 更多