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

Measuring distance between specific and current position with html5

本文关键字:位置 之间 距离 测量 定位 使用 html5      更新时间:2023-09-26

正如标题所暗示的那样。知道如何做到这一点吗?

一直在看 http://www.html5archive.com/2010/12/04/geolocation-api-example-distance-tracking/一段时间并试图修改该代码,但没有运气。

它最初不需要自动刷新。

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

密钥代码位于此页面上,与您指定的密钥链接。我已将其转换为以下函数:

/** 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 作为后两个参数传递给它。