# 获取两坐标之间距离

<!DOCTYPE html>

<html>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>登录</title>

<script>

var PI = Math.PI;

return d*PI/180.0;

}

/**

* caculate the great circle distance

* @param {Object} lat1

* @param {Object} lng1

* @param {Object} lat2

* @param {Object} lng2

*/

function getGreatCircleDistance(lat1,lng1,lat2,lng2){

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

return s;

}
/**

* approx distance between two points on earth ellipsoid

* @param {Object} lat1

* @param {Object} lng1

* @param {Object} lat2

* @param {Object} lng2

*/

function getFlatternDistance(lat1,lng1,lat2,lng2){

var f = getRad((lat1 + lat2)/2);

var g = getRad((lat1 - lat2)/2);

var l = getRad((lng1 - lng2)/2);

var sg = Math.sin(g);

var sl = Math.sin(l);

var sf = Math.sin(f);

var s,c,w,r,d,h1,h2;

var fl = 1/298.257;

sg = sg*sg;

sl = sl*sl;

sf = sf*sf;

s = sg*(1-sl) + (1-sf)*sl;

c = (1-sg)*(1-sl) + sf*sl;

w = Math.atan(Math.sqrt(s/c));

r = Math.sqrt(s*c)/w;

d = 2*w*a;

h1 = (3*r -1)/2/c;

h2 = (3*r +1)/2/s;

return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));

}
console.log(getGreatCircleDistance(39.87945682,116.64095684,39.91406638,116.60629272))
console.log(getFlatternDistance(39.87945682,116.64095684,39.91406638,116.60629272))
}

</script>
</body>

</html>
posted @ 2018-04-26 14:47  王汉炎  阅读(619)  评论(0编辑  收藏