Javascript 使用html5测量特定位置和当前位置之间的距离

Javascript 使用html5测量特定位置和当前位置之间的距离,javascript,html,geolocation,gps,Javascript,Html,Geolocation,Gps,正如标题所示。你知道怎么做到吗 我们已经研究了一段时间并试图修改代码,但没有成功 最初不需要自动刷新 但基本功能应能获取用户位置,并告知用户与特定点之间的距离。关键代码与您指定的代码链接在一起。我已将其转换为以下函数: /** Extend Number object with method to convert numeric degrees to radians */ if (typeof Number.prototype.toRadians == 'undefined') { Nu

正如标题所示。你知道怎么做到吗

我们已经研究了一段时间并试图修改代码,但没有成功

最初不需要自动刷新

但基本功能应能获取用户位置,并告知用户与特定点之间的距离。

关键代码与您指定的代码链接在一起。我已将其转换为以下函数:

/** 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;
}
您需要将特定的lat和lon作为前两个参数传递给它,将当前的lat和lon作为后两个参数。

并从页面底部获取toRad()函数,否则它工作正常!