自定义 海拔、经纬度、视高
var handler = new Cesium.ScreenSpaceEventHandler(proxy.viewer.scene.canvas);
const mouseOverHandler = function (movement: any) {
// 方法一
let endPosition = movement.endPosition;
// console.log(endPosition);
var ellipsoid = proxy.viewer.scene.globe.ellipsoid;
// console.log(ellipsoid);
var cartesian = proxy.viewer.camera.pickEllipsoid(movement.endPosition, ellipsoid);
// console.log(cartesian);
if (cartesian) {
//将笛卡尔坐标转换为地理坐标
var cartographic = ellipsoid.cartesianToCartographic(cartesian);
//将弧度转为度的十进制度表示
state.longitude = Cesium.Math.toDegrees(cartographic.longitude);
state.latitude = Cesium.Math.toDegrees(cartographic.latitude);
state.longitude = parseFloat(state.longitude.toFixed(6));
state.latitude = parseFloat(state.latitude.toFixed(6));
//获取高度
state.height = Math.ceil(proxy.viewer.camera.positionCartographic.height)
// state.height = parseFloat(state.height.toFixed(4));
// parseFloat
// console.log(cartographic);
// console.log(cartographic, longitude, latitude, height);
var altitude1 = proxy.viewer.scene.globe.getHeight(cartographic);
console.log(altitude1);
}
//俯仰角
state.pitch = Number(proxy.viewer.camera.pitch).toFixed(2);
//方向
state.heading = Number(proxy.viewer.camera.heading).toFixed(2);
// console.log(pitch, heading);
//方法二
var pick = new Cesium.Cartesian2(movement.endPosition.x, movement.endPosition.y);
console.log(pick);
let cartesian1 = proxy.viewer.scene.globe.pick(proxy.viewer.camera.getPickRay(pick), proxy.viewer.scene)
let cartographic1 = proxy.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian1)
var altitude = proxy.viewer.scene.globe.getHeight(cartographic1);
console.log(altitude);
//视角海拔高度
let he = Math.sqrt(
proxy.viewer.scene.camera.positionWC.x * proxy.viewer.scene.camera.positionWC.x +
proxy.viewer.scene.camera.positionWC.y * proxy.viewer.scene.camera.positionWC.y +
proxy.viewer.scene.camera.positionWC.z * proxy.viewer.scene.camera.positionWC.z
)
let he2 = Math.sqrt(
cartesian1.x * cartesian1.x + cartesian1.y * cartesian1.y + cartesian1.z * cartesian1.z
)
//地理坐标(弧度)转经纬度坐标
let point = [
(cartographic1.longitude / Math.PI) * 180,
(cartographic1.latitude / Math.PI) * 180,
]
if (!altitude) {
altitude = 0
}
if (!he) {
he = 0
}
if (!he2) {
he2 = 0
}
if (!point) {
point = [0, 0]
}
console.log("视高", (he - he2).toFixed(1));
console.log("经度", point[0].toFixed(6));
console.log("纬度", point[1].toFixed(6));
console.log("海拔", altitude.toFixed(2));
}
handler.setInputAction(mouseOverHandler, Cesium.ScreenSpaceEventType.MOUSE_MOVE)