Iphone 核心定位负值

Iphone 核心定位负值,iphone,objective-c,core-location,Iphone,Objective C,Core Location,我正在使用CoreLocation框架获取我的速度和距离,以计算平均速度 在CoreLocation发出的第一次更新中,速度和行驶距离均显示负值。我怎样才能解决这个问题 速度是locationController.locationManager.location.Speed,其中locationController保存我的CoreLocation委托。通过获取旧位置和新位置并计算距离来计算距离 //Distance - (void)locationManager:(CLLocationManag

我正在使用
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];
    }
}
下面有一个注释:

由于返回初始位置可能需要几秒钟的时间,因此位置管理器通常会立即交付以前缓存的位置数据,然后在可用时交付更多最新的位置数据。因此,在执行任何操作之前,最好检查任何位置对象的时间戳

您应该检查是否正在获取缓存值并忽略它,等待第一次“真正的”更新

NSTimeInterval timeSinceLastUpdate=[[newLocation timestamp]timeIntervalSinceNow];
//如果信息超过一分钟、两分钟或其他时间
//您需要基于预期的平均速度和期望的精度,
//不要用它。
如果(TimesInclastUpdate<-60){
返回;
}

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

// 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;
} 
//来自http://troybrant.net/blog/2010/02/detecting-bad-corelocation-data/
-(BOOL)isValidLocation:(CLLocation*)newLocation
withOldLocation:(CLLocation*)oldLocation
{
//过滤掉零个位置
如果(!newLocation){
返回否;
}
//按无效精度筛选出点
if(newLocation.HorizontalAccurance<0){
返回否;
}
//筛选出不符合顺序的点
NSTimeInterval secondsSinceLastPoint=[newLocation.timestamp
timeIntervalSinceDate:oldLocation.timestamp];
if(secondsSinceLastPoint<0){
返回否;
}
//筛选出在管理器初始化之前创建的点
NSTimeInterval secondsSinceManagerStarted=[newLocation.timestamp
timeIntervalSinceDate:locationManagerStartDate];
如果(secondsSinceManagerStarted<0){
返回否;
}
//newLocation很好用
返回YES;
} 

如果您正在采集第一个点,则它没有计算速度或距离的参考。我相信它的定义是返回-1,如果它不能满足您的需求

要解决这个问题,只需检查负值

if(location.speed > 0) {
    return;
}

这很好,但我还必须添加一些东西来防止负距离:
如果([newLocation distanceFromLocation:oldLocation]对不起,我是从snippets应用程序中获取的,当时没有URL。答案已更新。
if(location.speed > 0) {
    return;
}