如何计算纬度和经度指定的两点之间的距离?
为了澄清,我想要以公里为单位的距离; 这些要点使用WGS84系统,我想了解可用方法的相对准确性.
我正在尝试使用Haversine距离公式(如此处所示:http://www.movable-type.co.uk/scripts/latlong.html)但我无法使其工作,请参阅以下代码
function test() {
var lat2 = 42.741;
var lon2 = -71.3161;
var lat1 = 42.806911;
var lon1 = -71.290611;
var R = 6371; // km
//has a problem with the .toRad() method below.
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
alert(d);
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Uncaught TypeError: Object -0.06591099999999983 …Run Code Online (Sandbox Code Playgroud)