CoreLocation负值 [英] CoreLocation Negative Values

查看:91
本文介绍了CoreLocation负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CoreLocation 框架获取速度和距离以计算平均速度。

I am using the CoreLocation framework to get my speed and distance to calculate average speed.

CoreLocation 发出的第一次更新中,它显示的速度和行驶距离均为负值。我该如何解决?

On the first update that CoreLocation sends out, it shows negative values for both speed and distance traveled. How can I fix this?

速度为 locationController.locationManager.location.speed 其中 locationController 保存我的 CoreLocation 委托。通过获取旧位置和新位置并计算距离来计算距离。

Speed is locationController.locationManager.location.speed where locationController holds my CoreLocation delegate. Distance is calculated by taking the old location and the new location and calculating distance.

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


推荐答案

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

The data returned by Core Location may be invalid for a number of reasons. Before you use the data, run this method to see if it is valid.

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

这篇关于CoreLocation负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆