CoreLocation负值

Bau*_*aub 3 iphone objective-c core-location

我正在使用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];
    }
}
Run Code Online (Sandbox Code Playgroud)

Jan*_*ano 5

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

// 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;
} 
Run Code Online (Sandbox Code Playgroud)