Cesium使用经验——Cesium中根据线段两点经纬度坐标获取线段的偏航角heading并以计算得到的偏航角定位到线段

分类:计算机 | 三维开发 | WebGL | Cesium | 综合 1264
更新:2021-09-15 14:42:38
编辑

1 根据线段两点经纬度坐标获取线段的偏航角

//将WGS84经纬度坐标转换为笛卡尔空间直角坐标系坐标
var pointA=new Cesium.Cartesian3.fromDegrees(116.93698572, 30.70471922);
var pointB=new Cesium.Cartesian3.fromDegrees(116.93732431, 30.70417780);

var headDegree=Cesium.Math.toDegrees(getHeading(pointA,pointB));

function getHeading(pointA, pointB) {
    //建立以点A为原点,X轴为east,Y轴为north,Z轴朝上的坐标系
    var transform = Cesium.Transforms.eastNorthUpToFixedFrame(pointA);
    //向量AB
    var positionvector = Cesium.Cartesian3.subtract(pointB, pointA, new Cesium.Cartesian3());
    //因transform是将A为原点的eastNorthUp坐标系中的点转换到世界坐标系的矩阵
    //AB为世界坐标中的向量
    //因此将AB向量转换为A原点坐标系中的向量,需乘以transform的逆矩阵。
    var vector = Cesium.Matrix4.multiplyByPointAsVector(Cesium.Matrix4.inverse(transform, new Cesium.Matrix4()), positionvector, new Cesium.Cartesian3());
    //归一化
    var direction = Cesium.Cartesian3.normalize(vector, new Cesium.Cartesian3());
    //heading
    var heading = Math.atan2(direction.y, direction.x) - Cesium.Math.PI_OVER_TWO;
    return Cesium.Math.TWO_PI - Cesium.Math.zeroToTwoPi(heading);
}

2 以计算得到的偏航角定位到线段

        var postion= new this.Cesium.Cartesian3.fromDegrees(lon, lat, alt+50);
        var boundingSphere = new Cesium.BoundingSphere(postion, 0.0);
        this.viewer.camera.flyToBoundingSphere(boundingSphere, {
            duration: 3, //设置飞行持续时间,控制定位速度,单位是秒
            maximumHeight: undefined,
            complete: function () {
            },
            cancel: function () {
                console.log('定位取消!');
            },
            offset: {
                heading: Cesium.Math.toRadians(151.6139), //偏航角
                pitch: Cesium.Math.toRadians(-15), //俯仰角
                range: 150 //距目标点距离
            },
        });