使用CLLocationManager计算准确的总步行/跑步距离时遇到问题

时间:2012-01-08 16:15:20

标签: objective-c ios core-location cllocationmanager cllocation

我正在尝试构建一个iOS应用程序,显示跑步或步行时的总行进距离。我已经阅读并重新阅读了我能找到的所有文档,但是我无法想出能够给出准确总距离的文章。

与Nike + GPS或RunKeeper相比,我的应用程序始终报告距离较短。他们一开始会报告相同的情况,但随着我不断前进,我的应用与其他正在运行的应用的价值逐渐变化。

例如,如果我步行.3公里(通过我的汽车里程表验证),Nike + GPS和RunKeeper每次报告约.3公里,但我的应用程序将报告〜.13公里。 newLocation.horizo​​ntalAccuracy始终为5.0或10.0。

这是我正在使用的代码。我错过了一些明显的东西吗关于如何改进这一点以获得更准确的阅读的任何想法?

#define kDistanceCalculationInterval 10 // the interval (seconds) at which we calculate the user's distance
#define kNumLocationHistoriesToKeep 5 // the number of locations to store in history so that we can look back at them and determine which is most accurate
#define kValidLocationHistoryDeltaInterval 3 // the maximum valid age in seconds of a location stored in the location history
#define kMinLocationsNeededToUpdateDistance 3 // the number of locations needed in history before we will even update the current distance
#define kRequiredHorizontalAccuracy 40.0f // the required accuracy in meters for a location.  anything above this number will be discarded

- (id)init {
    if ((self = [super init])) {
        if ([CLLocationManager locationServicesEnabled]) {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
            self.locationManager.distanceFilter = 5; // specified in meters
        }

        self.locationHistory = [NSMutableArray arrayWithCapacity:kNumLocationHistoriesToKeep];
    }

    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // since the oldLocation might be from some previous use of core location, we need to make sure we're getting data from this run
    if (oldLocation == nil) return;
    BOOL isStaleLocation = [oldLocation.timestamp compare:self.startTimestamp] == NSOrderedAscending;

    [self.delegate locationManagerDebugText:[NSString stringWithFormat:@"accuracy: %.2f", newLocation.horizontalAccuracy]];

    if (!isStaleLocation && newLocation.horizontalAccuracy >= 0.0f && newLocation.horizontalAccuracy < kRequiredHorizontalAccuracy) {

        [self.locationHistory addObject:newLocation];
        if ([self.locationHistory count] > kNumLocationHistoriesToKeep) {
            [self.locationHistory removeObjectAtIndex:0];
        }

        BOOL canUpdateDistance = NO;
        if ([self.locationHistory count] >= kMinLocationsNeededToUpdateDistance) {
            canUpdateDistance = YES;
        }

        if ([NSDate timeIntervalSinceReferenceDate] - self.lastDistanceCalculation > kDistanceCalculationInterval) {
            self.lastDistanceCalculation = [NSDate timeIntervalSinceReferenceDate];

            CLLocation *lastLocation = (self.lastRecordedLocation != nil) ? self.lastRecordedLocation : oldLocation;

            CLLocation *bestLocation = nil;
            CGFloat bestAccuracy = kRequiredHorizontalAccuracy;
            for (CLLocation *location in self.locationHistory) {
                if ([NSDate timeIntervalSinceReferenceDate] - [location.timestamp timeIntervalSinceReferenceDate] <= kValidLocationHistoryDeltaInterval) {
                    if (location.horizontalAccuracy < bestAccuracy && location != lastLocation) {
                        bestAccuracy = location.horizontalAccuracy;
                        bestLocation = location;
                    }
                }
            }
            if (bestLocation == nil) bestLocation = newLocation;

            CLLocationDistance distance = [bestLocation distanceFromLocation:lastLocation];
            if (canUpdateDistance) self.totalDistance += distance;
            self.lastRecordedLocation = bestLocation;
        }
    }
}

2 个答案:

答案 0 :(得分:10)

事实证明,我上面发布的代码效果很好。问题碰巧发生在我的应用程序的不同部分。我不小心将距离从米转换为英里,而不是从米到公里。糟糕!

无论如何,希望我的帖子仍然有一些优点,因为我觉得它是如何跟踪用户与Core Location的距离的一个非常可靠的例子。

答案 1 :(得分:0)

您可能已将kRequiredHorizo​​ntalAccuracy设置得太低。如果历史中没有具有准确度的位置&lt; kRequiredHorizo​​ntalAccuracy,然后忽略所有这些点并将距离加0。