目标C - 这个大圆距离计算有什么问题?

时间:2012-01-11 21:16:17

标签: objective-c ios geolocation gps

我正在尝试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

1 个答案:

答案 0 :(得分:3)

确保传入的Location值不是nil或0,0。这似乎是你得到一个常数的原因是因为它正在进行数学计算,好像它是0°,0°。你离大约西非6218英里?如果是这样,你的函数工作得很好,但是调用它的方法在某些时候并没有提供真正的值。