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

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

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

为了解决这个问题,我不会在返回newLocation:之前调用stopUpdatingLocation newLocation:至less3次(这非常快)。 我还玩过了另外两个关于是否stopUpdatingLocation并返回newLocation “要求”。 我尝试的一种方法是检查纬度和长期的newLocation并与oldLocation比较,如果这些不相同,然后保持位置更新运行。 我也尝试检查oldLocationnewLocation之间的距离,如果它不到20米,那oldLocation 。 这两个testing与返回至less3次运行。 后一种方式不太“严格”,因为如果用户在移动的车辆中,新位置和oldLocation位置相当难以100%相同。

现在,我的问题是,即使在做上述(基本上不接受一个位置,直到在CLLocationManager几个更新,并检查CLLocations之间的距离(或是否相同),我仍然看到有点奇怪的结果的位置有时在testing时。

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

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

我不记得哪个项目,我从下面的代码中得到了,但是这对我来说真的很好(我记得它是从一个WWDC 2010的video)。 在我的代码中,我把原始项目的评论留下来,希望这会有所帮助。

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

从donkim的回答考虑replace这条线

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

与线

 [NSObject cancelPreviousPerformRequestsWithTarget: self];