1.获取当前屏幕中心点的位置
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.获取当前相机的位置
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.计算相机到屏幕中心点的距离
// 获取相机位置坐标
let a = this.getCenterPosition();
// 获取屏幕中心点位置坐标
let b = this.getCameraPosition();
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);
console.log('屏幕到地图中心距离:', distancetemp)
4.绕非固定屏幕中心点旋转
moveRotation方法参数中第一个参数step是移动步长,第二个参数i是判断是否是俯仰角还是航向角进行绕中心点进行翻滚,entity可以另外分离出来做一个方法,这样避免每次调用moveRotation方法都生成一个entity覆盖物。文章来源:https://www.toymoban.com/news/detail-801303.html
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);
}
5.相机位置在原地据法向量升起和下降
该方法的参数distance接受正负数,对应上升和下降文章来源地址https://www.toymoban.com/news/detail-801303.html
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);
}
到了这里,关于Cesium屏幕中心、相机坐标和距离计算的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!