2011-05-18 133 views

回答

9

关键代码是this page与您指定的代码链接。我已经将它转换成以下功能:

/** Extend Number object with method to convert numeric degrees to radians */ 
if (typeof Number.prototype.toRadians == 'undefined') { 
    Number.prototype.toRadians = function() { return this * Math.PI/180; }; 
} 

function getDistance(lat1,lon1,lat2,lon2) { 
    var R = 6371; // km 
    var dLat = (lat2-lat1).toRadians(); 
    var dLon = (lon2-lon1).toRadians(); 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
      Math.cos(lat1.toRadians()) * Math.cos(lat2.toRadians()) * 
      Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    return R * c; 
} 

您需要特定的纬度和经度把它作为前两个参数,而目前的纬度和经度作为第二两项。

+0

并从页面底部获取toRad()函数,否则它工作正常! – Xavio 2011-05-19 08:02:35

相关问题