如何计算"pure"在Swift的移动距离?

How calculate "pure" moving distance in Swift?

我编写了计算总行驶距离的应用程序。但我想让这个值更不准确。

我的问题是当我不动时,位置管理器总是更新位置并增加我的总距离 ~ 每秒 1...2 米 水平精度 10.0 。如果我停留/步行/停留,这个距离将每“站立”一分钟增加 60...100 米。这对我不好。

我可以停止使用 Core Motion 框架更新位置,并使用 CMMotionManager 检测 x、y、z 轴的变化。这将在我走路时起作用。但是当我开车时,设备不会改变 x、y、z 轴。

如何计算"pure"移动距离?

为 pedomter

声明一个 属性
@property (nonatomic, strong) CMPedometer *pedometer;

像这样初始化 目前支持这些功能的设备iphone 5s 及以上包含运动传感器芯片

 if ([CMPedometer isStepCountingAvailable]) {

            self.pedometer = [[CMPedometer alloc] init];
        }
        else {

            UIAlertView * alertView=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Distance calculation is not available on this device!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alertView show];
        }

并最终在此处理程序中获取数据

[self.pedometer startPedometerUpdatesFromDate:[NSDate date]
                                             withHandler:^(CMPedometerData *pedometerData, NSError *error) {
                                                dispatch_async(dispatch_get_main_queue(), ^{

                                                   NSLog(@"data:%@, error:%@", pedometerData, error);
                                                   NSLog(@"distance:%@, pedometerData.distance);
                                                    NSLog(@"Steps:%@", pedometerData.numberOfSteps);

                                                });
                                             }];

不要忘记导入 coremotion 框架