通过两个坐标,获取这两个坐标点中间的坐标点
// 已知的两个坐标点
pointArr = [new BMap.Point(113.854687, 22.502549), new BMap.Point(113.8429946899414, 22.499418258666992)];
// 获取两个坐标点之间的位置
getMidpoiont() {
const lngca =
(Math.max(parseFloat(this.pointArr[0].lng), parseFloat(this.pointArr[1].lng)) -
Math.min(parseFloat(this.pointArr[0].lng), parseFloat(this.pointArr[1].lng))) /
2;
const latca =
(Math.max(parseFloat(this.pointArr[0].lat), parseFloat(this.pointArr[1].lat)) -
Math.min(parseFloat(this.pointArr[0].lat), parseFloat(this.pointArr[1].lat))) /
2;
const lngcenter = Math.min(parseFloat(this.pointArr[0].lng), parseFloat(this.pointArr[1].lng)) + lngca;
const latcenter = Math.min(parseFloat(this.pointArr[0].lat), parseFloat(this.pointArr[1].lat)) + latca;
const pointcenter = new BMap.Point(lngcenter, latcenter);
return pointcenter;
}
通过两个坐标获取两个坐标连线的角度
getAngle() {
// 船尾 116.329898,39.964613
const next = {
point: {
latitude: 116.329898,
longitude: 39.964613,
},
};
// 船头 116.549516,39.930539
const n = {
point: {
latitude: 116.549516,
longitude: 39.930539,
},
};
let ret;
const w1 = (n.point.latitude / 180) * Math.PI;
const j1 = (n.point.longitude / 180) * Math.PI;
const w2 = (next.point.latitude / 180) * Math.PI;
const j2 = (next.point.longitude / 180) * Math.PI;
ret = 4 * Math.pow(Math.sin((w1 - w2) / 2), 2) - Math.pow(Math.sin((j1 - j2) / 2) * (Math.cos(w1) - Math.cos(w2)), 2);
ret = Math.sqrt(ret);
const temp = Math.sin((j1 - j2) / 2) * (Math.cos(w1) + Math.cos(w2));
ret = ret / temp;
ret = (Math.atan(ret) / Math.PI) * 180;
ret += 90;
if (j1 - j2 < 0) {
if (w1 - w2 < 0) {
// tslint:disable-next-line:no-unused-expression
ret;
} else {
ret = -ret + 180;
}
} else {
if (w1 - w2 < 0) {
ret = 180 + ret;
} else {
ret = -ret;
}
}
return 180 - ret;
}