<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js"></script>
<script type="text/javascript">
var EARTH_RADIUS = 6378137.0; //单位M
var PI = Math.PI;
function getRad(d) {
return d * PI / 180.0;
}
/**
* caculate the great circle distance
* @param {Object} lat1(纬度1)
* @param {Object} lng1(经度1)
* @param {Object} lat2(纬度2)
* @param {Object} lng2(经度2)
*/
//第一种方法:这种算法是把地球当作规则的球面来计算的咯,这种方法不是很精准咯,这个还要取决你定位的精准度咯
function getGreatCircleDistance(lat1, lng1, lat2, lng2) {
var radLat1 = getRad(lat1);
var radLat2 = getRad(lat2);
var a = radLat1 - radLat2;
var b = getRad(lng1) - getRad(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 * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000.0;
alert(s);
}
//第一种方法:地球是椭圆的,所以会有这种算法
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 a = EARTH_RADIUS;
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;
alert( d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg)));
}
$(function () {
getFlatternDistance(28.89596, 105.443985, 28.896462, 105.444291);
});
</script>
</head>
<body>
63.3422
</body>
</html>