1.笛卡尔坐标转换为经纬度坐标
const getLngLatByCartesian3 = (car3_ps: any, type: any) => {
let result = null;
if (car3_ps instanceof Cesium.Cartesian3) {
let _cartographic = Cesium.Cartographic.fromCartesian(car3_ps);
let _lat = Cesium.Math.toDegrees(_cartographic.latitude);
let _lng = Cesium.Math.toDegrees(_cartographic.longitude);
let _alt = _cartographic.height;
if (type == "polyline") {
result = { longitude: _lng, latitude: _lat, elevation: _alt };
} else {
// result = { lng: _lng, lat: _lat, alt: _alt }
result = { lng: _lng, lat: _lat };
}
return result;
} else if (car3_ps instanceof Array) {
let res = [];
for (let i = 0; i < car3_ps.length; i++) {
let _cartographic = Cesium.Cartographic.fromCartesian(car3_ps[i]);
let _lat = Cesium.Math.toDegrees(_cartographic.latitude);
let _lng = Cesium.Math.toDegrees(_cartographic.longitude);
let _alt = _cartographic.height;
if (type == "polyline") {
res.push({ longitude: _lng, latitude: _lat, elevation: _alt });
} else {
// res.push({ lng: _lng, lat: _lat, alt: _alt })
res.push({ lng: _lng, lat: _lat });
}
}
return res;
}
}
2.二维屏幕坐标转换为三维笛卡尔坐标
const getCartesian3FromPixel=(viewer:any, px:any)=> {
let cartesian = null;
let isOn3DTiles = false;
let isOnTerrain = false;// 使用 drillPick 获取拾取结果
let picks = viewer.scene.drillPick(px);for (let i in picks) {
let pick = picks[i];// 判断拾取到的 primitive 类型,包括 3D Tiles、3D Tileset 和模型
if (
(pick && pick.primitive instanceof Cesium.Cesium3DTileFeature) ||
(pick && pick.primitive instanceof Cesium.Cesium3DTileset) ||
(pick && pick.primitive instanceof Cesium.Model)
) {
// 如果在 3D Tiles 上拾取到位置点
isOn3DTiles = true;
return viewer.scene.pickPosition(px);
}
}// 判断是否存在地形
let noTerrain =
viewer.terrainProvider instanceof Cesium.EllipsoidTerrainProvider;// 在地形上拾取位置点
if (!isOn3DTiles && !noTerrain) {
let ray = viewer.scene.camera.getPickRay(px);
if (!ray) return null;
isOnTerrain = true;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}// 在普通地球上拾取位置点
if (!isOn3DTiles && !isOnTerrain && noTerrain) {
cartesian = viewer.scene.camera.pickEllipsoid(
px,
viewer.scene.globe.ellipsoid
);
}return cartesian;
}
3.经纬度坐标转换为笛卡尔坐标
const convertCoordinatesToCartesian=(coordinates:any)=> {
const longitude = coordinates[0];
const latitude = coordinates[1];
const height = coordinates.length > 2 ? coordinates[2] : 0;// 使用 Cesium 提供的方法将经纬度坐标转换为笛卡尔坐标系
return Cesium.Cartesian3.fromDegrees(longitude, latitude, height)}
4.经纬度坐标转换为屏幕坐标
interface IBtnData {
left: number
top: number
}
const getBtnLeftOrTop = (position: number[], viewer: Cesium.Viewer): IBtnData | null => {
const posi = Cesium.Cartesian3.fromDegrees(position[0], position[1], 0);
const chanedc = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene, posi);
if (chanedc == undefined) return null;
return { left: Math.round(chanedc.x), top: Math.round(chanedc.y) }
}
欢迎补充,一起进步。