我正在尝试计算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
Oracle的实施显然是关闭的,但我不明白为什么。任何帮助将不胜感激。
答案 0 :(得分:6)
尝试颠倒纵坐标的顺序,如:
SELECT sdo_geom.sdo_distance(sdo_geom.sdo_geometry(2001, 8307, sdo_geom.sdo_point_type(-74.033907, 40.716715, NULL), NULL, NULL),
sdo_geom.sdo_geometry(2001, 8307, sdo_point_type(-74.033901, 40.716300, NULL), NULL, NULL), 0.0001, 'unit=M') distance_in_m
from DUAL;
我得到46.087817955912。
使用SDO_GEOMETRY时,纵坐标以X,Y(经度,纬度)顺序列出。