2009-07-24 36 views
1

我正在玩iPhone SDK,我想在我的应用程序中显示当前的速度。有这么多的应用程序可以做到非常精确,特别是对于跑步或骑自行车等低速运动。我见过的最好的是RunKeeper。为什么我的CLLocation速度不准确?

但是,在我的应用程序中,速度是绝对不准确的。在低速时它总是无效的,只有在更高的速度下它才会显示一些数值,但它们很少被更新并且不是很有用。

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation { 
    if (newLocation.timestamp > oldLocation.timestamp && 
     newLocation.verticalAccuracy > 0.0f &&         // GPS is active 
     newLocation.horizontalAccuracy < 500.0f &&         // GPS is active 
     //newLocation.horizontalAccuracy < kCLLocationAccuracyHundredMeters && // good quality GPS signal 
     //newLocation.speed > 1.0f &&           // enough movment for accurate speed and course measurement 
     oldLocation != nil)              // oldLocation is nil on the first reading 
    { 
     double speed = (newLocation.speed * 3.6); 
     [self updateDisplayWithSpeed:speed]; 
     //double direction = newLocation.course; 
    } 
} 

有没有人有工作代码?或者你能告诉我我的问题吗?

回答

7

我会尝试以下操作。

请记住,您应该根据您的场景需要设置distanceFilter和desiredAccuracy:行走与驾车等不同。此外,当您从GPS请求高精度时,必须始终丢弃提供的第一个位置通过GPS并使用第二个作为起始位置。引用Apple文档:

您应该为此属性指定一个适合您的使用场景的值。换句话说,如果您只需要几公里内的当前位置,则不应指定kCLLocationAccuracyBest来确保准确性。以更高的准确度确定位置需要更多的时间和更多的权力。 请求高准确度的位置数据时,位置服务提供的初始事件可能没有您请求的准确性。定位服务尽可能快地提供初始事件。然后,它会继续根据您请求的准确性确定位置,并在有数据可用时根据需要提供其他事件。

这是我的建议。

首先,使用GPS更新位置,间隔至少1秒。低于此间隔时,速度值无用,因为它以米/秒为单位进行测量,并且由于之前的讨论,您不可能在一秒钟内得到有效更新并且精确度较高。

其次,只对位置坐标使用有意义的值:您应该丢弃具有负水平准确性的值。这就是我说的好地点时所指的。第三,你可以自己计算距离:使用getDistanceFrom()计算上一个好位置和上一个好位置之间的距离,然后除以位置更新之间经过的秒数。这会给你以米/秒为单位的距离。我会尽力做到这一点,并将结果与​​Apple提供的速度进行比较。

+0

你有一个代码示例来处理第一个问题?或者,这只是检查与前一个位置的时间戳? – 2014-03-20 08:40:25

3

请尝试在你的代码如下:

speedLabel.text = [NSString stringWithFormat:@"SPEED(Km/Hr): %f", [location speed]*3.6]; 
latitudeLabel.text = [NSString stringWithFormat:@"LATITUDE: %f", location.coordinate.latitude]; 
longitudeLabel.text = [NSString stringWithFormat:@"LONGITUDE: %f", location.coordinate.longitude]; 
CLLocationDistance meters = [location distanceFromLocation:orginalLoc]; 
distanceLabel.text = [NSString stringWithFormat:@"DISTANCE(M): %f", meters];