使用CoreLocation防止偶发坐标

时间:2012-03-14 18:09:42

标签: iphone ios gps core-location

基本上我正在创建一个应该记录某人旅行的路径和数据的应用程序。我在准确性和确定不准确的报告方面遇到了一些麻烦。这是我的iPhone记录的路径图像。红线是记录的路径,粉红色是我实际行走的方式(对于巨大的屏幕截图道歉):

Path

我的代表记录数据如下:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (newLocation.horizontalAccuracy < 0) return;
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    // The "> 50.0f" is to hopefully throw out inaccurate points
    if (locationAge > 5.0 || [newLocation distanceFromLocation:oldLocation] > 50.0f) return;
    [[self locationPoints] addObject:newLocation];
    [self updateView];
}

我看到的一些建议是使用过滤器来检查数据是否适合投影路径。这是最好的方式吗?

1 个答案:

答案 0 :(得分:1)

您可以添加newLocation.horizontalAccuracy的检查:

if ((newLocation.horizontalAccuracy > 0.0) && (newLocation.horizontalAccuracy < 10.0))

或者,对于您的情况:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (newLocation.horizontalAccuracy < 0) return;
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    
    if ((locationAge > 5.0)||(newLocation.horizontalAccuracy < 0.0)||(newLocation.horizontalAccuracy > 10.0)) return;
    [[self locationPoints] addObject:newLocation];
    [self updateView];
}

(locationAge > 5.0)条件似乎很奇怪。你真的想要吗?如果您将其保留在应用中,则会在设备丢失>5.0s的正确GPS信号时停止更新。

还有一件事:你有没有想过用你的locationManager设置:

locationManager.desiredAccuracy=kCLLocationAccuracyBest;

你也应该知道,这会耗尽大量电池,但需要进行测试......