用javascript写一个方法,求出两个经纬度之间的中心点
/**
* 根据输入的地点坐标计算中心点
* @param beginAxis 起点坐标 "经度,纬度"
* @param endAxis 终点坐标 "经度,纬度"
* @return 返回中心点坐标 "经度,纬度"
*/
function getCenterPoint(beginAxis,endAxis) {
var arr = [beginAxis,endAxis];
var total = arr.length;
var X = 0, Y = 0, Z = 0;
for (var i = 0; i < arr.length; i++) {
var lat, lon, x, y, z;
lon = (arr[i].split(",")[0]) * Math.PI / 180;
lat = (arr[i].split(",")[1]) * Math.PI / 180;
x = Math.cos(lat) * Math.cos(lon);
y = Math.cos(lat) * Math.sin(lon);
z = Math.sin(lat);
X += x;
Y += y;
Z += z;
}
X = X / total;
Y = Y / total;
Z = Z / total;
var Lon = Math.atan2(Y, X);
var Hyp = Math.sqrt(X * X + Y * Y);
var Lat = Math.atan2(Z, Hyp);
var centreAxis = (Lon * 180 / Math.PI) + "," + (Lat * 180 / Math.PI);
return centreAxis;
}
如何使用
var cen = getCenterPoint(
"116.368904, 39.913423",
"116.382122, 39.901176"
);
console.log("cen === ", cen);
提示:本代码是javascript写的,可以根据需求自行调整,如果对你有帮助记得点赞,感谢!