地球表面两点距离计算

//地球半径,单位为公里

var EARTH_RADIUS = 6378.137;

//计算弧度

rad : function (degree) {

    return degree * Math.PI / 180.0;

},

//计算两个经纬度坐标之间的距离,返回单位为公里的数值

calDistance : function (lat1, lng1, lat2, lng2) {

    var radLat1 = this.rad(lat1);

    var radLat2 = this.rad(lat2);

    var a = radLat1 - radLat2;

    var b = this.rad(lng1) - this.rad(lng2);

    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));

    s = s * this.EarthRadius/1000;

    s = Math.round(s * 10000) / 10000;

    return s;

},

//测试:计算从上海和北京两点之间的距离

alert(calDistance(31.22, 121.48, 39.90, 116.40));

/*函数说明如下:

1、公式中经纬度均用弧度表示;

2Lat1 Lng1 分别表示A点经、纬度,Lat2 Lng2  分别表示B点经纬度;

3a=Lat1 Lat2 为两点纬度之差  b=Lng1 -Lng2 为两点经度之差;

46378.137为地球半径,单位为公里;

*/


猜你喜欢

转载自blog.51cto.com/31329846/2123419