我正在尝试计算 Oracle DB 中两点之间的距离,
Point A is 40.716715, -74.033907
Point B is 40.716300, -74.033900
使用这个 sql 语句:
SELECT sdo_geom.sdo_distance( sdo_geom.sdo_geometry(2001 ,8307 ,sdo_geom.sdo_point_type(40.716715, -74.033907 , NULL) ,NULL ,NULL)
,sdo_geom.sdo_geometry(2001 ,8307 ,sdo_point_type(40.716300,-74.033901, NULL) ,NULL ,NULL) ,0.0001 ,'unit=M') distance_in_m
from DUAL;
结果是12.7646185977151
使用 Apple 的 CoreLocation api 执行此操作时:
CLLocation* pa = [[CLLocation alloc] initWithLatitude:40.716715 longitude:-74.033907];
CLLocation* pa2 = [[CLLocation alloc] initWithLatitude:40.716300 longitude:-74.033900];
CLLocationDistance dist = [pa distanceFromLocation:pa2];
结果是46.0888946842423
自己的实现
double dinstance_m(double lat1, double long1, double lat2, double long2){
double dlong = (long2 - long1) * d2r;
double dlat = (lat2 - lat1) * d2r;
double a = pow(sin(dlat/2.0), 2) + cos(lat1*d2r) * cos(lat2*d2r) * pow(sin(dlong/2.0), 2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = 6367 * c;
return d * 1000.;
}
结果是 46.120690774231
甲骨文的实施显然是关闭的,但我不明白为什么。任何帮助将不胜感激。