我已经实现了下面的代码,通过使用CLLocationDistance方法将所有点对点距离相加(累加)来计算总距离。我创建了一个计数器(_locManagerIteration),并在第一次迭代中将第一个locationManager对象复制到我的pastCoordinate属性中。在下一次迭代中,我使用CLLocationDistance方法来计算pastCoordinate对象和新的loc对象之间的点对点距离。在程序运行中,我看到_pointDistance变量(double)正在收集距离,但总距离(_totalDistance)变量(也是double)始终与_pointDistance变量相同(即没有累积距离)。由于某种无法解释的原因,语句_totalDistance = _totalDistance + _pointDistance要么没有执行,要么_totalDistance变量始终为零。下面的两个NSLog语句显示了相同的结果...你能帮帮我吗?
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
_locManagerIteration++;
CLLocation* loc = [locations lastObject];
if (_locManagerIteration == 1) {
self.pastCoordinate = [locations lastObject];
} else {
CLLocationDistance _pointDistance = [self.pastCoordinate distanceFromLocation:loc];
NSLog(@"POINT TO POINT DISTANCE = %f", _pointDistance);
_totalDistance = _totalDistance + _pointDistance;
self.pastCoordinate = [locations lastObject];
}
NSLog(@"TOTAL DISTANCE = %f", _totalDistance);发布于 2014-10-08 03:42:11
你观察到了多少次迭代?在做这个实验的时候,你真的很用力吗?你正在使用的模式下的GPS可能真的不准确,特别是在封闭的空间中。然后,新的位置可能总是出现在相同的位置,而您最终会添加零。
https://stackoverflow.com/questions/26243113
复制相似问题