1.获取当前屏幕中心点的位置
javascript
getCenterPosition() {
let centerResult = viewer.camera.pickEllipsoid(
new Cesium.Cartesian2(
viewer.canvas.clientWidth / 2,
viewer.canvas.clientHeight / 2,
),
)
centerPoint = centerResult;
let curPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(centerResult);
let curLongitude = (curPosition.longitude * 180) / Math.PI;
let curLatitude = (curPosition.latitude * 180) / Math.PI;
return {
lon: curLongitude,
lat: curLatitude,
}
// }
}
2.获取当前相机的位置
javascript
getCameraPosition() {
var position = viewer.scene.camera.positionCartographic;
// 弧度转经纬度
var longitude = Cesium.Math.toDegrees(position.longitude);
var latitude = Cesium.Math.toDegrees(position.latitude);
var height = position.height;
return { lng: longitude, lat: latitude, h: height }
}
3.绕非固定屏幕中心点旋转
moveRotation方法参数中第一个参数step是移动步长,第二个参数i是判断是否是俯仰角还是航向角进行绕中心点进行翻滚,entity可以另外分离出来做一个方法,这样避免每次调用moveRotation方法都生成一个entity覆盖物。
javascript
moveRotation(step, i = 0) {
// 添加实体
let a = this.getCenterPosition();
let b = this.getCameraPosition();
entity = viewer.entities.add({
position: new Cesium.Cartesian3.fromDegrees(a.lon, a.lat, 0),
point: {
pixelSize: 0,
// show: false
}
});
let clickPosition1 = Cesium.Cartesian3.fromDegrees(a.lon, a.lat, 0);
let clickPosition2 = Cesium.Cartesian3.fromDegrees(b.lng, b.lat, b.h);
// 计算两个点之间的距离
let distancetemp = Cesium.Cartesian3.distance(clickPosition1, clickPosition2);
// 距离远一些就将移动角度调整大一些
if (i && distancetemp > 20000) {
heading += 3 * step;
} else if (i) {
heading += step;
} else if (!i && distancetemp > 20000) {
pitch += 3 * step;
} else if (!i) {
pitch += step;
}
let offset = new Cesium.HeadingPitchRange(
heading,
pitch,
distancetemp // 相机距离地球球心的距离
);
viewer.zoomTo(entity, offset);
}
4.相机位置在原地据法向量升起和下降
该方法的参数distance接受正负数,对应上升和下降
javascript
moveForward(distance) {
var camera = viewer.camera;
var direction = camera.direction;
//获得此位置默认的向上方向
var up = Cesium.Cartesian3.normalize(camera.position, new Cesium.Cartesian3());
var right = Cesium.Cartesian3.cross(direction, up, new Cesium.Cartesian3());
direction = Cesium.Cartesian3.cross(up, right, new Cesium.Cartesian3());
direction = Cesium.Cartesian3.normalize(direction, direction);
direction = Cesium.Cartesian3.multiplyByScalar(direction, distance, direction);
camera.position = Cesium.Cartesian3.add(camera.position, direction, camera.position);
}