使用html5测量特定位置和当前位置之间的距离
问题描述:
正如标题所示。任何想法如何实现?使用html5测量特定位置和当前位置之间的距离
一直在寻找http://www.html5archive.com/2010/12/04/geolocation-api-example-distance-tracking/一段时间,并试图修改该代码,但没有运气。
它最初不需要自动刷新。
但基本功能应该获取用户位置并告诉用户与特定点之间的距离。
答
关键代码是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;
}
您需要特定的纬度和经度把它作为前两个参数,而目前的纬度和经度作为第二两项。
并从页面底部获取toRad()函数,否则它工作正常! – Xavio 2011-05-19 08:02:35