计算两个经纬度点之间的距离?
Calculate distance between two latitude-longitude points?
我需要计算两个 GPS 点之间的距离。我在之前的 post:
中找到了这个函数
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
我试过了,还是不行。假设我没有移动,因此 lat1
和 lat2
是一样的。因此,dLat
和 dLon
都将为 0。因此,术语 a
将为 0。术语 c = 2 * Math.atan2(0,1) = 3.141593
(我使用 Excel 来获取此数字),和距离 d = 20015.08 km
。
我正在计算给定两个 GPS 点的车辆每秒移动的距离。我期待的是少数。这里有什么问题?
我使用了这段代码 - 它运行良好。
public static double distance(MyLocation p1, MyLocation p2) {
double theta = p1.mLongitude - p2.mLongitude;
String unit = "K";
double dist = Math.sin(deg2rad(p1.mLatitude)) * Math.sin(deg2rad(p2.mLatitude))
+ Math.cos(deg2rad(p1.mLatitude)) * Math.cos(deg2rad(p2.mLatitude)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == "K") {
dist = dist * 1.609344;
} else if (unit == "N") {
dist = dist * 0.8684;
}
return (dist);
}
它以公里为单位计算距离。
您使用的公式称为 Haversine 公式。
它工作得很好,也特别适合短距离工作。
我检查了你的代码和我的实现,我发现你的实现有一个错误
您使用的半正弦公式应该使用以米为单位的地球半径,为什么 R 使用千米?
您的问题:
"Term c = 2 * Math.atan2(0,1) = 3.141593 (I used Excel to get this number)"
这是你的假设错误。 c = 0 项。很高兴,您提到了 Excell。 Excell 对 atan2() 的参数使用相反的顺序,在 excell 中你必须写 atan2(1,0).
进一步:
如果将 R 校正为单位米无助于确保
所有变量都是双精度类型。
当我看到你的代码时,我担心某个地方 double 会悄悄地转换为 int。
如果这一切都没有帮助:
请显示 lat1、lon1、lat2、lon2 的示例输入以及 a 和 c 的中间值。并确保您的 deg2rad() 有效。 (将 R 更正为米后)
我需要计算两个 GPS 点之间的距离。我在之前的 post:
中找到了这个函数function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
我试过了,还是不行。假设我没有移动,因此 lat1
和 lat2
是一样的。因此,dLat
和 dLon
都将为 0。因此,术语 a
将为 0。术语 c = 2 * Math.atan2(0,1) = 3.141593
(我使用 Excel 来获取此数字),和距离 d = 20015.08 km
。
我正在计算给定两个 GPS 点的车辆每秒移动的距离。我期待的是少数。这里有什么问题?
我使用了这段代码 - 它运行良好。
public static double distance(MyLocation p1, MyLocation p2) {
double theta = p1.mLongitude - p2.mLongitude;
String unit = "K";
double dist = Math.sin(deg2rad(p1.mLatitude)) * Math.sin(deg2rad(p2.mLatitude))
+ Math.cos(deg2rad(p1.mLatitude)) * Math.cos(deg2rad(p2.mLatitude)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == "K") {
dist = dist * 1.609344;
} else if (unit == "N") {
dist = dist * 0.8684;
}
return (dist);
}
它以公里为单位计算距离。
您使用的公式称为 Haversine 公式。
它工作得很好,也特别适合短距离工作。
我检查了你的代码和我的实现,我发现你的实现有一个错误
您使用的半正弦公式应该使用以米为单位的地球半径,为什么 R 使用千米?
您的问题:
"Term c = 2 * Math.atan2(0,1) = 3.141593 (I used Excel to get this number)"
这是你的假设错误。 c = 0 项。很高兴,您提到了 Excell。 Excell 对 atan2() 的参数使用相反的顺序,在 excell 中你必须写 atan2(1,0).
进一步:
如果将 R 校正为单位米无助于确保
所有变量都是双精度类型。
当我看到你的代码时,我担心某个地方 double 会悄悄地转换为 int。
如果这一切都没有帮助:
请显示 lat1、lon1、lat2、lon2 的示例输入以及 a 和 c 的中间值。并确保您的 deg2rad() 有效。 (将 R 更正为米后)