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,
];
}
cesium 相机围绕视图中心点旋转
最新推荐文章于 2024-09-15 01:26:49 发布