【问题标题】:Objective C - What is wrong with this great circle distance calculation?目标 C - 这个大圆距离计算有什么问题?
【发布时间】:2012-01-11 21:16:16
【问题描述】:

我正在尝试执行great circle distance calculation。正如您应该能够收集到的,Location 类具有计算中列出的属性。

- (NSNumber *)greatCircleDistanceFrom:(Location *)other
{
    // Unpack all the NSNumbers into doubles so we can manipulate them
    double selfCosRadLat = [self.cosRadLat doubleValue];
    double otherCosRadLat = [other.cosRadLat doubleValue];
    double selfRadLng = [self.radLng doubleValue];
    double otherRadLng = [other.radLng doubleValue];
    double selfSinRadLat = [self.sinRadLat doubleValue];
    double otherSinRadLat = [other.sinRadLat doubleValue];

    // Multiplying by 3959 calculates the distance in miles.
    double d = acos(selfCosRadLat
                    * otherCosRadLat
                    * cos(selfRadLng - otherRadLng)
                    + selfSinRadLat
                    * otherSinRadLat
                    ) * 3959.0;

    return [NSNumber numberWithDouble:d];
}

在我运行单元测试的一半时间里,我得到了正确的值。另一半,我得到6218.78265778

【问题讨论】:

  • 可以使用CLLocation内置的distanceFromLocation方法。
  • 那使用大圆距离公式吗?

标签: objective-c ios geolocation gps


【解决方案1】:

确保您传入的 Location 值不是 nil 或 0,0。看起来你会得到这样一个常数的原因是因为它正在做数学,就好像它在 0°,0°。你离西非大约 6218 英里吗?如果是这样,您的函数运行良好,但调用它的方法有时无法提供实际值。

【讨论】:

  • 其实,谢谢。我现在看到计算失败,因为值是 0,0。但是,它们不应该是 0,0。我现在遇到了 Core Data 的新问题...
  • 至少你不再有这个问题了:)
  • 是的,new problem
猜你喜欢
  • 2022-09-29
  • 1970-01-01
  • 2011-01-06
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
相关资源
最近更新 更多