将仪表转换为英里不准确的结果

时间:2014-02-10 19:26:18

标签: objective-c

我正在尝试将行进的仪表转换为英里数。我遇到的问题是结果完全不准确,我找不到原因。

以下是我正在使用的代码:

CLLocation* newLocation = [locations lastObject];

NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow];

if (age > 120) return;    //

if (newLocation.horizontalAccuracy < 0) return;


if (self.oldLocation == nil || self.oldLocation.horizontalAccuracy < 0) {
    self.oldLocation = newLocation;
    return;
}

CLLocationDistance distance = [newLocation distanceFromLocation: self.oldLocation];

NSLog(@"%6.6f/%6.6f to %6.6f/%6.6f for %2.0fm, accuracy +/-%2.0fm",
      self.oldLocation.coordinate.latitude,
      self.oldLocation.coordinate.longitude,
      newLocation.coordinate.latitude,
      newLocation.coordinate.longitude,
      distance,
      newLocation.horizontalAccuracy);

NSLog(@"distance is %f", distance);
totalDistanceBetween =+ (distance * 0.000621371192);
NSString *cycleDistanceString = [[NSString alloc]
                                 initWithFormat:@"%f meters",
                                 totalDistanceBetween];

_CurrentDistance.text = cycleDistanceString;
    self.oldLocation = newLocation;

任何人都可以告诉我哪里出错了吗?

由于

1 个答案:

答案 0 :(得分:3)

您的代码说明了这一点:

totalDistanceBetween =+ (distance * 0.000621371192);

使用一元+运算符,它不执行任何操作。你似乎意味着这个:

totalDistanceBetween += (distance * 0.000621371192);

此外,distance的单位是米(因为这是distanceFromLocation:返回的内容),totalDistanceBetween的单位是英里,因为0.000621371192英里= 1米。但在下一个语句中,您的格式字符串显示为%f meters。应该说%f miles