我使用CoreLocation框架来获取我的速度和距离来计算平均速度。
CoreLocation发出的第一次更新中,它显示速度和行驶距离的负值。我该怎么解决?
速度是locationController.locationManager.location.speed其中locationController包含我的CoreLocation代表。距离是通过取旧位置和新位置并计算距离来计算的。

//Distance
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    // make sure the old and new coordinates are different
    if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
        (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
    {
        mDistance = [newLocation distanceFromLocation:oldLocation];
    }
}

最佳答案

由于多种原因,核心位置返回的数据可能无效。在使用数据之前,请运行此方法以查看它是否有效。

// From http://troybrant.net/blog/2010/02/detecting-bad-corelocation-data/
- (BOOL)isValidLocation:(CLLocation *)newLocation
        withOldLocation:(CLLocation *)oldLocation
{
    // filter out nil locations
    if (!newLocation){
        return NO;
    }
    // filter out points by invalid accuracy
    if (newLocation.horizontalAccuracy < 0){
        return NO;
    }
    // filter out points that are out of order
    NSTimeInterval secondsSinceLastPoint = [newLocation.timestamp
                                            timeIntervalSinceDate:oldLocation.timestamp];
    if (secondsSinceLastPoint < 0){
        return NO;
    }
    // filter out points created before the manager was initialized
    NSTimeInterval secondsSinceManagerStarted = [newLocation.timestamp
                                                 timeIntervalSinceDate:locationManagerStartDate];
    if (secondsSinceManagerStarted < 0){
        return NO;
    }
    // newLocation is good to use
    return YES;
}

10-08 05:51