I am trying to do the great circle distance calculation. As you should be able to glean, the Location
class has the properties listed in the calculation.
- (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];
}
Half the time I run my unit test, I get the right value. The other half, I get 6218.78265778
.