3

我正在使用 iPhone SDK,我想在我的应用程序中显示当前速度。有很多应用程序可以做到这一点非常精确,特别是对于跑步或骑自行车等低速行驶。我见过的最好的是RunKeeper。

但是,在我的应用程序中,速度绝对不准确。在低速时它始终为空,只有在高速时它才会显示一些值,但它们很少更新并且不是真正有用的。

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

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

4

2 回答 2

7

我会尝试以下。

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

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

这是我的建议。

首先,使用 GPS 以至少一秒的间隔更新位置。在此间隔之下,速度值是无用的,因为它是以米每秒为单位测量的,并且由于前面的讨论,您不太可能在不到一秒的时间内以高精度获得有效更新。

其次,仅对位置坐标使用有意义的值:您应该丢弃具有负水平精度的值。这就是我在谈到好位置时所指的。

第三,您可以自己计算距离:使用 getDistanceFrom() 计算上一个好位置和上一个好位置之间的距离,然后除以位置更新之间经过的秒数。这将为您提供以米/秒为单位的距离。我将尝试这样做并将结果与​​ Apple 提供的速度进行比较。

于 2009-07-25T09:00:22.393 回答
4

请在您的代码中尝试以下操作:

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];
于 2012-03-22T07:04:43.273 回答