CLLocationManager和准确性问题 - 任何经验?

run*_*mad 27 cllocationmanager cllocation ios

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

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

为了解决这个问题,我不会stopUpdatingLocation在它返回newLocation:至少3次之前打电话(这很快).我还玩过两个关于是否stopUpdatingLocation返回的"要求" newLocation.我尝试的一种方法是检查lat和long newLocation并进行比较oldLocation,如果这些不相同,则保持位置更新运行.我也试图与检查之间的距离oldLocationnewLocation,如果它小于20米,它的罚款.这两个都是通过返回至少3次运行来测试的.后一种方式不那么"严格",因为如果用户在移动的车辆中,newLocation并且oldLocation很难与100%完全相同.

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

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

人们习惯于解决同类问题的经验和可能的解决方案?评论和解决方案赞赏:)

don*_*kim 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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

通过GetLocationViewController.mApple的"Locate Me"示例项目,可在以下位置获得:

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