最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Measuring distance between specific and current position with html5 - Stack Overflow

programmeradmin5浏览0评论

As the title suggests. Any idea how to acplish that?

Been looking at / for a while and trying to modify that code, but with no luck.

It doesn't need to be auto refreshing initially.

But basic function should get user location and tell distance between the user and the specific point.

As the title suggests. Any idea how to acplish that?

Been looking at http://www.html5archive./2010/12/04/geolocation-api-example-distance-tracking/ for a while and trying to modify that code, but with no luck.

It doesn't need to be auto refreshing initially.

But basic function should get user location and tell distance between the user and the specific point.

Share Improve this question asked May 18, 2011 at 15:05 XavioXavio 4371 gold badge6 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

The key code is on this page linked from the one you specified. I've converted it to a function below:

/** 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;
}

You need to pass it the specific lat and lon as the first two parameters, and the current lat and lon as the second two.

发布评论

评论列表(0)

  1. 暂无评论