我正在使用l80 GPS模块,使用GPS坐标计算距离。当我检查办公室的总距离时,有时会正确显示" 0"但有时它显示" 9223372036854775808.775808"有时它显示" 2.18654"。
这是我使用的代码:
double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
//double theta, dist;
theta = lon1 - lon2;
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta));
dist = acos(dist);
dist = rad2deg(dist);
//dist = dist * 60 * 1.1515;
dist = dist * 60 * 1.609344;
dist+=dist;
return (dist);
}
double deg2rad(double deg) {
return (deg * pi / 180);
}
double rad2deg(double rad) {
return (rad * 180 / pi);
}
以下是我的输出示例:
old_lat:13.006952 old_lon:80.257065 new_lat:13.006952 new_lon:80.257065 Total distance: 0.000000
old_lat:13.006949 old_lon:80.257065 new_lat:13.006948 new_lon:80.257065Total distance: 9223372036854775808.775808