在绘制矩形的时候,确定对角两点就可画出矩形,根据这两点可以计算出相应的其他两点的坐标
定义三角点集合
var points = [
new Cesium.Cartesian3(0, 0, 0), // 第一个顶点
new Cesium.Cartesian3(10, 0, 0), // 第二个顶点
new Cesium.Cartesian3(5, 5, 0) // 第三个顶点
];
调用 Rectangle.fromCartesianArray() 函数计算矩形边界
var rectangle = Cesium.Rectangle.fromCartesianArray(points);
输出结果
console.log("矩形左上角经度:" + rectangle.west);
console.log("矩形右下角经度:" + rectangle.east);
console.log("矩形左上角纬度:" + rectangle.north);
console.log("矩形右下角纬度:" + rectangle.south);
创建四个顶点的位置
var A = Cesium.Cartesian3.fromDegrees(lon1, lat1); // 左上角经纬度
var B = Cesium.Cartesian3.fromDegrees(lon2, lat1); // 右上角经纬度
var C = Cesium.Cartesian3.fromDegrees(lon2, lat2); // 右下角经纬度
var D = Cesium.Cartesian3.fromDegrees(lon1, lat2); // 左下角经纬度
计算AB、BC、CD、DA的长度
var lengthAB = Cesium.Cartesian3.distance(A, B);
var lengthBC = Cesium.Cartesian3.distance(B, C);
var lengthCD = Cesium.Cartesian3.distance(C, D);
var lengthDA = Cesium.Cartesian3.distance(D, A);
计算矩形的长度
var rectangleLength = (lengthAB + lengthBC + lengthCD) / 2;
console.log("矩形的长度为:" + rectangleLength);
绘制矩形
const rectangleHand = Cesium.Rectangle.fromCartesianArray(clickPoint)
viewer.entities.add({
name: 'drawRectangle',
rectangle: {
coordinates: rectangleHand,
material: Cesium.Color.fromCssColorString("rgba(25, 98, 134, 0.5)"),
},
});
删除移动矩形
removeEntityByName('moveDrawRectangle')
const northwest = Cesium.Rectangle.northwest(rectangleHand);//西北角弧度坐标(左上)
const southwest = Cesium.Rectangle.southwest(rectangleHand);//西南角弧度坐标(左下)
const northeast = Cesium.Rectangle.northeast(rectangleHand);//东北角弧度坐标(右上)
const southeast = Cesium.Rectangle.southeast(rectangleHand);//东南角弧度坐标(右下)
转换为经纬度
const leftTop = [Cesium.Math.toDegrees(northwest.longitude), Cesium.Math.toDegrees(northwest.latitude)];//左上角经度
const leftBottom = [Cesium.Math.toDegrees(southwest.longitude), Cesium.Math.toDegrees(southwest.latitude)];//左下角经度
const rightTop = [Cesium.Math.toDegrees(northeast.longitude), Cesium.Math.toDegrees(northeast.latitude)];//右上角经度
const rightBottom = [Cesium.Math.toDegrees(southeast.longitude), Cesium.Math.toDegrees(southeast.latitude)];//右下角经度
var polygon = turf.polygon([[
leftTop, rightTop, rightBottom, leftBottom, leftTop
]]);