【发布时间】:2023-12-10 12:46:02
【问题描述】:
我使用了 CLLocation 的 distanceFromLocation: 方法来计算与某个位置的距离。
但是当我使用“Haversine公式”计算时,结果略有不同。
- (CLLocationDistance)distanceFromCoordinate:(CLLocationCoordinate2D)fromCoord {
double earthRadius = 6371.0; // Earth's radius in Kilometers
// Get the difference between our two points then convert the difference into radians
double nDLat = RADIANS((fromCoord.latitude - self.coordinate.latitude));
double nDLon = RADIANS((fromCoord.longitude - self.coordinate.longitude));
double fromLat = RADIANS(self.coordinate.latitude);
double toLat = RADIANS(fromCoord.latitude);
double nA = pow ( sin(nDLat/2.0), 2 ) + cos(fromLat) * cos(toLat) * pow ( sin(nDLon/2.0), 2 );
double nC = 2.0 * atan2( sqrt(nA), sqrt( 1 - nA ));
double nD = earthRadius * nC;
return nD * 1000.0;
}
CLLocation * loc = [[CLLocation alloc] initWithLatitude:[location.latitude doubleValue]
longitude:[location.longitude doubleValue]];
CLLocationDistance dist = [userLocation distanceFromLocation:loc];
CLLocationDistance dist2 = [userLocation distanceFromCoordinate:loc.coordinate];
为什么两个值不同?
我应该用horizontalAccuracy和verticalAccuracy初始化位置对象吗?
【问题讨论】:
-
您有没有找到使
distanceFromLocation匹配Haversine 公式的方法?我遇到了同样的问题。
标签: ios objective-c distance core-location cllocation