- 筛选出缓存(旧)的位置数据,
- 筛选出无效的位置结果(精度< 0),
- 集精度要求设置为
kCLLocationAccuracyBest
,
- 设置的最小距离过滤器,优选在至少10米以过滤位置噪音。
- 编辑:当
oldLocation
为零时,不计算距离。
还有更多你可以做,但这应该工作,只要你得到足够好的horizonalAccuracy(< 30米)。您可以过滤掉低精度的结果,但是您必须自己跟踪一下这个更复杂一点的oldLocation。
添加NSLog(见下文),以便了解发生了什么。如果你仍然没有得到好的结果发布的NSLog的输出,所以我们可以看到发生了什么,并帮助更多。
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow];
if (age > 120) return; // ignore old (cached) updates
if (newLocation.horizontalAccuracy < 0) return; // ignore invalid udpates
// EDIT: need a valid oldLocation to be able to compute distance
if (oldLocation == nil || oldLocation.horizontalAccuracy < 0) return;
CLLocationDistance distance = [newLocation distanceFromLocation: oldLocation];
NSLog(@"%6.6f/%6.6f to %6.6f/%6.6f for %2.0fm, accuracy +/-%2.0fm",
oldLocation.coordinate.latitude,
oldLocation.coordinate.longitude,
newLocation.coordinate.latitude,
newLocation.coordinate.longitude,
distance,
newLocation.horizontalAccuracy);
distanceMoved += distance;
theLabel.text = [NSString stringWithFormat: @"%f meters", distanceMoved];
}
- (void)viewDidLoad
{
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyBest;
locMan.distanceFilter = 10;
[locMan startUpdatingLocation];
isInitial = true;
distanceMoved = 0.0;
[super viewDidLoad];
}
编辑iOS6的,如果你想要做的使用didUpdateLocations:(因为上面的方法现在已经过时)最简单的办法是简单地在属性保存每个位置,这样你有下一次更新相同以前的位置。声明一个属性的类来保存oldLocation
:
@property (nonatomic, retain) CLLocation* oldLocation;
然后在委托方法您将每个newLocation
用作oldLocation
下一次委托方法被称为:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocations:(NSArray *)locations
{
CLLocation* newLocation = [locations lastObject];
NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow];
if (age > 120) return; // ignore old (cached) updates
if (newLocation.horizontalAccuracy < 0) return; // ignore invalid udpates
// EDIT: need a valid oldLocation to be able to compute distance
if (self.oldLocation == nil || self.oldLocation.horizontalAccuracy < 0) {
self.oldLocation = newLocation;
return;
}
CLLocationDistance distance = [newLocation distanceFromLocation: self.oldLocation];
NSLog(@"%6.6f/%6.6f to %6.6f/%6.6f for %2.0fm, accuracy +/-%2.0fm",
self.oldLocation.coordinate.latitude,
self.oldLocation.coordinate.longitude,
newLocation.coordinate.latitude,
newLocation.coordinate.longitude,
distance,
newLocation.horizontalAccuracy);
distanceMoved += distance;
theLabel.text = [NSString stringWithFormat: @"%f meters", distanceMoved];
self.oldLocation = newLocation; // save newLocation for next time
}
我刚刚使用了这个确切的代码,但我将距离过滤器更改为1.出于某种原因,我的距离从-1开始。这里是我的输出行走后大约2米前进: 2012-04-17 17:58:05.341 LocationManagerTest2 [422:707] 0.000000/0.000000至39.856110/-74.940113 -1m,精度+/- 10m 2012-04 -17 17:58:10.248 LocationManagerTest2 [422:707] 39.856110/-74.940113到39.856165/-74.940107为6米,准确度+/- 50m 2012-04-17 17:58:11.248 LocationManagerTest2 [422:707] 39.856165/- 74.940107到39.856223/-74.940010为10米,准确度为+/- 50m 距离移动到了15米左右 - 太多了 – bnasty 2012-04-17 22:00:37
我之前也试过使用这段代码,并且我得到了一些看似准确的结果,远离 – bnasty 2012-04-17 22:05:10
-1距离是您oldLocation为0的位置,这可能意味着oldLocation参数为零。您还需要添加一个支票(请参阅上面的编辑)。报告水平时准确度不高,准确度为50m!这意味着GPS没有足够好的信号,这意味着报告的位置可以向任何方向偏离50米。 – progrmr 2012-04-18 01:31:12