js版-胡老师
google.maps.LatLng.prototype.distanceFrom = function(latlng) {var lat = [this.lat(), latlng.lat()]
var lng = [this.lng(), latlng.lng()]
var R = 6378137;
var dLat = (lat[1] - lat[0]) * Math.PI / 180;
var dLng = (lng[1] - lng[0]) * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat[0] * Math.PI / 180) * Math.cos(lat[1] * Math.PI / 180) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return Math.round(d);
}
一、经纬度算两点距离-百度api-精确度第一
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=FB33d26d18536d1ee0712c4f01bf2123&s=1"></script>
<title>经纬度算两点距离-百度api-精确度第一</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap"); // 创建Map实例
var point1 = new BMap.Point(116.357428, 39.90923);
var point2 = new BMap.Point(116.397428, 39.90923);
var distance =map.getDistance(point1, point2);
console.log(distance)
</script>
二、经纬度算两点距离-误差2米-精确度第二
<!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 runat="server">
<title>经纬度算两点距离-误差2米-精确度第二</title>
<script type="text/javascript">
var DEF_PI = 3.14159265359; // PI
var DEF_2PI = 6.28318530712; // 2*PI
var DEF_PI180 = 0.01745329252; // PI/180.0
var DEF_R = 6370693.5; // radius of earth
function getShortDistance(lon1, lat1, lon2, lat2) {
var ew1, ns1, ew2, ns2;
var dx, dy, dew;
var distance;
// 角度转换为弧度
ew1 = lon1 * DEF_PI180;
ns1 = lat1 * DEF_PI180;
ew2 = lon2 * DEF_PI180;
ns2 = lat2 * DEF_PI180;
// 经度差
dew = ew1 - ew2;
// 若跨东经和西经180 度,进行调整
if (dew > DEF_PI)
dew = DEF_2PI - dew;
else if (dew < -DEF_PI)
dew = DEF_2PI + dew;
dx = DEF_R * Math.cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度)
dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度)
// 勾股定理求斜边长
distance = Math.sqrt(dx * dx + dy * dy).toFixed(0);
console.log(distance)
return distance;
}
getShortDistance(116.357428,39.90923,116.397428,39.90923)
</script>
</head>
<body>
</body>
</html>
三、经纬度算两点距离-误差4米-精确度第三
<!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 runat="server">
<title>经纬度算两点距离-误差4米-精确度第三</title>
<script type="text/javascript">
function distanceByLnglat(lng1,lat1,lng2,lat2)
{
var radLat1 = Rad(lat1);
var radLat2 = Rad(lat2);
var a = radLat1 - radLat2;
var b = Rad(lng1) - 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 * 6378137.0;// 取WGS84标准参考椭球中的地球长半径(单位:m)
s = Math.round(s * 10000) / 10000;
alert(s);
// //下面为两点间空间距离(非球面体)
// var value= Math.pow(Math.pow(lng1-lng2,2)+Math.pow(lat1-lat2,2),1/2);
// alert(value);
}
function Rad(d)
{
return d * Math.PI / 180.0;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div onclick ="distanceByLnglat(116.357428, 39.90923,116.397428, 39.90923);">
test me
</div>
</form>
</body>
</html>
四、经纬度算两点距离-误差1000米-精确度第四
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>经纬度算两点距离-误差1000米-精确度第四</title>
</head>
<body>
</body>
<script type="text/javascript">
var EARTH_RADIUS = 6378137.0; //地球半径
var PI = Math.PI;
function getRad(d){
return d*PI/180.0;
}
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;
var dis=d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
console.log(dis);
return dis;
}
getFlatternDistance(116.357428,39.90923,116.397428,39.90923)
</script>
</html>
五、经纬度算两点距离-误差1000米-精确度第五
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>经纬度算两点距离-误差1000米-精确度第五</title>
</head>
<body>
</body>
<script type="text/javascript">
var EARTH_RADIUS = 6378137.0; //地球半径
var PI = Math.PI;
function getRad(d){
return d*PI/180.0;
}
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;
console.log(s);
return s;
}
getGreatCircleDistance(116.357428,39.90923,116.397428,39.90923)
</script>
</html>