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

Iphone iOS:locationManager:didUpdateToLocation:fromLocation:报告不移动时的速度,iphone,ios,gps,core-location,cllocationmanager,Iphone,Ios,Gps,Core Location,Cllocationmanager,我正在使用locationManager:didUpdateToLocation:fromLocation:获取位置更新。大多数情况下,一切正常,但当我检查newLocation.speed属性时,站着不动时,我几乎总能得到大于0的速度值。即使我第一次开始得到位置更新,甚至从未移动过,在几次更新后,我也会得到正值 只需执行一个简单的大于0检查,然后设置一个UILabel,如果它是: if (newLocation.speed > 0) { self.speed.text = [N

我正在使用locationManager:didUpdateToLocation:fromLocation:获取位置更新。大多数情况下,一切正常,但当我检查newLocation.speed属性时,站着不动时,我几乎总能得到大于0的速度值。即使我第一次开始得到位置更新,甚至从未移动过,在几次更新后,我也会得到正值

只需执行一个简单的大于0检查,然后设置一个UILabel,如果它是:

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
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
//首先检查我们是否得到了我们想要的准确度
if(newLocation.horizontalcreaction<0)返回;
//确保更新是新的,而不是缓存的
NSTimeInterval locationAge=-[newLocation.timestamp timeintervalncesInnow];
如果(位置>5.0)返回;
//检查旧的和新的是否相同
if((oldLocation.coordinate.latitude==newLocation.coordinate.latitude)&&
(oldLocation.coordinate.longitude==newLocation.coordinate.longitude))返回;
//确保我们的新位置离旧位置不太远
CLLocationDistance距离=[newLocation distanceFromLocation:oldLocation];
NSLog(@“距离:%f”,距离);
如果(距离>40)返回;
//确保我们有一个新的位置
如果(!newLocation)返回;
//检查一下我们是否被拦住了
如果(newLocation.speed==0){
self.inMotion=否;
}否则如果(newLocation.speed>0){
self.inMotion=是;
}否则{
//可能是无效的负值
self.inMotion=否;
}//结束
//速度
//即使未录制,也应始终更新!
如果(newLocation.speed>0&&inMotion){
self.speed.text=[NSString stringWithFormat:@“%.2f”,[self-speed:newLocation.speed]];
NSLog(@“速度:%f”,newLocation.Speed);
}
//下面是我为这篇文章删除的更多代码
}//结束

这正常吗?

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

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


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

我已经用locationManager:didUpdateToLocation:fromLocation:method开头的代码更新了我的帖子。现在我正在检查更新之间的距离是否超过40米,如果是,请不要使用它们。这个值应该小得多吗?当我进行修复时,它们相距约5-6米。改进我的想法?