iOS:locationManager:didUpdateToLocation:fromLocation:报告不移动时的速度

发布于 2024-12-07 02:50:13 字数 1971 浏览 1 评论 0原文

我正在使用 locationManager:didUpdateToLocation:fromLocation: 来获取位置更新。大多数情况下一切都工作正常,但是当我静止不动地检查 newLocation.speed 属性时,我几乎总是得到大于 0 的速度值。即使当我第一次开始获取位置更新并且从未移动过时,我也会在之后得到正值一些更新。

只需进行一个简单的大于 0 的检查,然后设置一个 UILablel(如果是):

if (newLocation.speed > 0) {
     self.speed.text = [NSString stringWithFormat:@"%.2f",[self speed:newLocation.speed]];
}

来自该方法的更多代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    // Check first if we are getting the accuracy we want
    if (newLocation.horizontalAccuracy < 0) return;

    // Make sure the update is new not cached
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;

    // Check to see if old and new are the same
    if ((oldLocation.coordinate.latitude == newLocation.coordinate.latitude) &&
        (oldLocation.coordinate.longitude == newLocation.coordinate.longitude)) return;

    // Make sure our new location is not super far away from old
    CLLocationDistance dist = [newLocation distanceFromLocation:oldLocation];
    NSLog(@"Distance: %f", dist);
    if (dist > 40) return;

    // Make sure we have a new location
    if (!newLocation) return;

    // Check to see if we are stopped
    if (newLocation.speed == 0) {
        self.inMotion = NO;
    } else if (newLocation.speed > 0) {
        self.inMotion = YES;
    } else {
        // Probably an invalid negative value
        self.inMotion = NO;
    }//end

    // Speed
    // Should always be updated even when not recording!
    if (newLocation.speed > 0 && inMotion) {
        self.speed.text = [NSString stringWithFormat:@"%.2f",[self speed:newLocation.speed]];
        NSLog(@"Speed: %f", newLocation.speed);
    }

    // Tons more code below that I removed for this post

}//end

这是正常的吗?

I am using locationManager:didUpdateToLocation:fromLocation: to get location updates. Most everything is working well but when I check the newLocation.speed propery while standing still I almost always get a speed value greater than 0. Even when I start getting location updates for the first time and have NEVER even moved I will get positive values after a few updates.

Just doing a simple greater than 0 check then setting a UILablel if it is:

if (newLocation.speed > 0) {
     self.speed.text = [NSString stringWithFormat:@"%.2f",[self speed:newLocation.speed]];
}

More code from that method:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    // Check first if we are getting the accuracy we want
    if (newLocation.horizontalAccuracy < 0) return;

    // Make sure the update is new not cached
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;

    // Check to see if old and new are the same
    if ((oldLocation.coordinate.latitude == newLocation.coordinate.latitude) &&
        (oldLocation.coordinate.longitude == newLocation.coordinate.longitude)) return;

    // Make sure our new location is not super far away from old
    CLLocationDistance dist = [newLocation distanceFromLocation:oldLocation];
    NSLog(@"Distance: %f", dist);
    if (dist > 40) return;

    // Make sure we have a new location
    if (!newLocation) return;

    // Check to see if we are stopped
    if (newLocation.speed == 0) {
        self.inMotion = NO;
    } else if (newLocation.speed > 0) {
        self.inMotion = YES;
    } else {
        // Probably an invalid negative value
        self.inMotion = NO;
    }//end

    // Speed
    // Should always be updated even when not recording!
    if (newLocation.speed > 0 && inMotion) {
        self.speed.text = [NSString stringWithFormat:@"%.2f",[self speed:newLocation.speed]];
        NSLog(@"Speed: %f", newLocation.speed);
    }

    // Tons more code below that I removed for this post

}//end

Is this normal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他是夢罘是命 2024-12-14 02:50:13

比零大多少?持续多久?您正在比较的所有位置是否具有相同的精度?

在 GPS 接收器中,速度通常由连续定位之间的位置变化确定(也可以通过测量多普勒频移来计算)。由于测量误差或变得更加准确,位置修复会略有不同。位置的变化可能会使设备看起来像是移动了​​。

例如,假设您的第一个定位点的水平精度为 1000m,第二个定位点的水平精度为 300m,则第一个位置可能距实际位置 1000m,第二个位置可能距第一个定位点 700m,即使设备尚未移动。这转化为位置随时间的变化,即“速度”。

How much greater than zero? And for how long? Do all the locations that you are comparing have the same accuracy?

In a GPS receiver the speed is usually determined by the change in location between successive location fixes (it can also be computed by measuring doppler shift). Location fixes will vary slightly due to measurement errors or as they become more accurate. The changes in location can make it appear that the device moved.

For example, suppose your first fix has an accuracy of 1000m horizontal and your second fix has an accuracy of 300m horizontal, the first position could have been 1000m off of the real location and the second could be 700m away from that first fix even though the device hasn't moved. This translates to a change in location over time which is "speed".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文