【问题标题】:CoreLocation Negative ValuesCoreLocation 负值
【发布时间】:2011-11-01 17:57:29
【问题描述】:

我正在使用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];
    }
}

【问题讨论】:

    标签: iphone objective-c core-location


    【解决方案1】:

    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;
    } 
    

    【讨论】:

    • 这很好用,但我还必须添加一些东西来防止负距离:if ([newLocation distanceFromLocation:oldLocation] &lt;0) { return NO; }
    • 抱歉,我是从我的 sn-ps 应用程序中获得的,当时没有 URL。答案已更新。
    【解决方案2】:

    CLLocationManager Class Reference中有注释:

    由于返回初始位置可能需要几秒钟的时间,因此位置管理器通常会立即提供以前缓存的位置数据,然后在可用时提供更多最新的位置数据。因此,在采取任何行动之前检查任何位置对象的时间戳总是一个好主意。

    你应该检查你是否得到一个缓存值并忽略它,等待第一次“真正的”更新。

    NSTimeInterval timeSinceLastUpdate = [[newLocation timestamp] timeIntervalSinceNow];
    
    // If the information is older than a minute, or two minutes, or whatever
    // you want based on expected average speed and desired accuracy, 
    // don't use it.
    if( timeSinceLastUpdate < -60 ){
        return;
    }
    

    【讨论】:

      【解决方案3】:

      如果您正在收集第一个点,则它没有参考来计算速度或距离。我相信如果它不能得到你需要的东西,它被定义为返回 -1。

      要解决此问题,只需检查负值即可。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-30
        • 2013-07-18
        • 2015-02-28
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多