cesium 相机围绕视图中心点旋转


        document.querySelector('.rotation1').addEventListener("click", () => {
            // ro(2)
            rotationCamera(5)
        })
        document.querySelector('.rotation2').addEventListener("click", () => {
            rotationCamera(-5)

        })

        function rotationCamera(n) {
            let centerScreen = getScreenCenterCoordinates();
            console.log(centerScreen, 'getScreenCenterCoordinates');
            // 获取当前相机
            const camera = viewer.camera;
            // console.log(camera.position, 'camera');

            let p = Cartesian3_to_WGS84(camera.position)
            // console.log(p, 'ppp');
            // 获取当前相机的方向(heading)
            var point1 = turf.point(centerScreen);
            var point2 = turf.point(p);
            var options = { units: 'meters' };
            var bearing = turf.bearing(point1, point2);
            var bearing1 = turf.bearing(point2, point1);
            console.log(bearing, bearing1, 'bearing1bearing1');
            var distance = turf.distance(point1, point2, options);
            // console.log(Cesium.Math.toRadians(n), 'Cesium.Math.toRadians(n)Cesium.Math.toRadians(n)');
            let newBearing = bearing + n;
            console.log(newBearing, 'newBearing');

            var destination = turf.destination(point1, distance, newBearing, options);
            let newP = destination.geometry.coordinates
            newP.push(p[2])
           
            let bearingCamera = 0;
            if (newBearing >= 0) {
                bearingCamera = newBearing - 180
            } else {
                bearingCamera = newBearing + 180
            }
            // 将相机旋转到新的方向
            camera.setView({
                destination: Cesium.Cartesian3.fromDegrees(newP[0], newP[1], newP[2]),
                orientation: {
                    heading: Cesium.Math.toRadians(bearingCamera),
                    pitch: camera.pitch,
                    roll: camera.roll,
                },
            });
        }

        function getScreenCenterCoordinates() {
           
            // 获取场景
            var scene = viewer.scene;

            // 获取屏幕中心的坐标
            var canvas = scene.canvas;
            var center = new Cesium.Cartesian2(canvas.clientWidth / 2, canvas.clientHeight / 2);

            // 获取椭球上的点
            var ellipsoid = scene.globe.ellipsoid;
            var centerPosition = scene.camera.pickEllipsoid(center, ellipsoid);

            // 将椭球上的点转换为经纬度
            var centerCartographic = Cesium.Cartographic.fromCartesian(centerPosition);
            var centerLongitude = Cesium.Math.toDegrees(centerCartographic.longitude);
            var centerLatitude = Cesium.Math.toDegrees(centerCartographic.latitude);
            // 获取高度
            let height = (viewer.camera.positionCartographic.height).toFixed(2);
            let coordinates = [centerLongitude, centerLatitude, height];
            return coordinates;
        }
        function Cartesian3_to_WGS84(point) {
            var cartesian3 = new Cesium.Cartesian3(point.x, point.y, point.z);
            var cartographic = Cesium.Cartographic.fromCartesian(cartesian3);
            var lat = Cesium.Math.toDegrees(cartographic.latitude);
            var lng = Cesium.Math.toDegrees(cartographic.longitude);
            var alt = cartographic.height.toFixed(2);
            return [
                lng,
                lat,
                alt,
            ];
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值