如何计算两个纬度和经度之间的距离
How to calculate the distance between two latitude and longitude
我正在尝试使用纬度和经度计算温哥华和多伦多之间的距离。我正在使用哈弗正弦公式。我预计距离约为 4390 公里。谁能告诉我我犯了什么错误。这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body onload="getDistance()">
<script>
// Convert Degress to Radians
function Deg2Rad( deg ) {
return deg * Math.PI / 180;
}
function getDistance()
{
//Toronto Latitude 43.74 and longitude -79.37
//Vancouver Latitude 49.25 and longitude -123.12
lat1 = Deg2Rad(43.74);
lat2 = Deg2Rad(49.25);
lon1 = Deg2Rad(-79.37);
lon2 = Deg2Rad(-123.12);
latDiff = lat2-lat1;
lonDiff = lon2-lon1;
var R = 6371000; // metres
var φ1 = lat1;
var φ2 = lat2;
var Δφ = Deg2Rad(latDiff);
var Δλ = Deg2Rad(lonDiff);
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
alert('d: ' + d);
var dist = Math.acos( Math.sin(φ1)*Math.sin(φ2) + Math.cos(φ1)*Math.cos(φ2) * Math.cos(Δλ) ) * R;
alert('dist: ' + dist);
}
</script>
</body>
</html>
当我运行此代码时,我得到完全不同的数字。
我认为你应该期待更像3358公里。 http://www.distancefromto.net/distance-from/Toronto/to/Vancouver
这是正确结果的小提琴:https://jsfiddle.net/Lk1du2L1/
在这种情况下,如果你从次要结果中删除你的deg2rad,你就是正确的 - 你这样做太频繁了:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body onload="getDistance()">
<script>
// Convert Degress to Radians
function Deg2Rad( deg ) {
return deg * Math.PI / 180;
}
function getDistance()
{
//Toronto Latitude 43.74 and longitude -79.37
//Vancouver Latitude 49.25 and longitude -123.12
lat1 = Deg2Rad(43.74);
lat2 = Deg2Rad(49.25);
lon1 = Deg2Rad(-79.37);
lon2 = Deg2Rad(-123.12);
latDiff = lat2-lat1;
lonDiff = lon2-lon1;
var R = 6371000; // metres
var φ1 = lat1;
var φ2 = lat2;
var Δφ = latDiff;
var Δλ = lonDiff;
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
alert('d: ' + d);
var dist = Math.acos( Math.sin(φ1)*Math.sin(φ2) + Math.cos(φ1)*Math.cos(φ2) * Math.cos(Δλ) ) * R;
alert('dist: ' + dist);
}
</script>
</body>
</html>
相关文章:
- “可绘制地图”设置地图选项“纬度-经度”
- 如何用javascript获取谷歌地图的经度和纬度
- 使用API在我的网站Google-Map上显示搜索地址的纬度和经度
- 计算php或js中的距离
- 使用谷歌地图api计算距离并将其设置在表格中<td>
- 使用传单在地图框上显示纬度和经度
- 如何在谷歌地图api v3中获得无公里驾驶距离
- 如何使用谷歌地图中的纬度和经度搜索附近的位置
- 铯JS-两点之间的距离
- 如何在不重新加载整个网页的情况下动态更改经度和纬度的值
- 地理位置和计算从当前纬度/液化天然气到另一纬度/液化石油气的距离
- cloudSearch:范围从0到某个值之间的距离
- 如何通过javascript从jsongooglemapapi中获取地理编码纬度和经度
- 计算数据点阵列与平均值的最大距离,并返回相关数据点
- 如何使用XML中的经度和纬度值在地图上显示位置
- 如何计算两个纬度和经度之间的距离
- 使用距离查找纬度和经度
- 如何使用我的位置来计算两点之间的距离使用纬度和经度
- 通过提供源和目标 .NET 或 JavaScript 的纬度和经度来计算两个位置之间的道路距离
- 过滤城市的纬度和经度列表在给定的距离从另一个城市(与lat/long)