【发布时间】:2014-08-15 17:37:12
【问题描述】:
这是我基于this 和this question 的答案的Haversine 公式代码:
$scope.getCoordDistance = function (myLat, myLon, locLat, locLon) {
var lat2 = 41.894993;
var lon2 = -88.459239;
var lat1 = $scope.locLat;
var lon1 = $scope.locLon;
var R = 3959;
var x1 = lat1 - lat2;
var dLat = x1 * Math.PI / 180;
var x2 = lon1 - lon2;
var dLon = x2 * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
d = R * c;
return d;
}
我用 3 个不同的位置对此进行了测试,它们都相距甚远,第一个位置表示其距离为 14790.7 英里,而实际距离为 44.1 英里,每个位置的偏差量都不同。有些是少量的,有些是非常大的。我的数学有问题吗?如果没有,为什么这段代码不能正常工作?
这是我完整项目的链接:http://plnkr.co/edit/nRQc7Ym0lsaK6jQwd626?p=preview
非常感谢您的帮助!!!
【问题讨论】:
标签: javascript angularjs object coordinates haversine