cesium绘制矩形并返回矩形经纬度及⾯积
使⽤turf.js库计算⾯积
// 绘制矩形
function drawRectangle(whetherStart) {
if (viewer) {
// drawPoint(fal)
if (whetherStart) {
let clickPoint = [];
removeEntityByName('drawRectangle')
return new Promi((resolve, reject) => {
// ⿏标左键绘制
viewer.screenSpaceEventHandler.tInputAction(function (movement) {
const earthPosition = viewer.camera.pickEllipsoid(movement.position, viewer.scene.globe.ellipsoid);//当前世界坐标笛卡尔积
if (Cesium.defined(earthPosition)) {
// 判断现在是第⼏个点
if (clickPoint.length === 0) {//还没有点,添加第⼀个点
clickPoint.push(earthPosition)
} el {//绘制矩形
clickPoint.push(earthPosition)
// 绘制矩形
const rectangleHand = Cesium.Rectangle.fromCartesianArray(clickPoint)
name: 'drawRectangle',
rectangle: {
coordinates: rectangleHand,
material: Cesium.Color.fromCssColorString("rgba(25, 98, 134, 0.5)"),
},
});
左肩膀有痣的女人// 删除移动矩形
removeEntityByName('moveDrawRectangle')
const northwest = hwest(rectangleHand);//西北⾓弧度坐标(左上)
const southwest = Cesium.Rectangle.southwest(rectangleHand);//西南⾓弧度坐标(左下)
const northeast = heast(rectangleHand);//东北⾓弧度坐标(右上)
const southeast = Cesium.Rectangle.southeast(rectangleHand);//东南⾓弧度坐标(右下)
// 转换为经纬度
const leftTop = [Degrees(northwest.longitude), Degrees(northwest.latitude)];//左上⾓经度
const leftBottom = [Degrees(southwest.longitude), Degrees(southwest.latitude)];//左下⾓经度 const rightTop = [Degrees(northeast.longitude), Degrees(northeast.latitude)];//右上⾓经度
const rightBottom = [Degrees(southeast.longitude), Degrees(southeast.latitude)];//右下⾓经度 var polygon = turf.polygon([[
leftTop, rightTop, rightBottom, leftBottom, leftTop
]]);
// ⾯积(m²)
var area = turf.area(polygon);
const data = {
leftTop: leftTop,
leftBottom: leftBottom,
rightTop: rightTop,
rightBottom: rightBottom,
area: area,//⾯积(m²)
}婴儿咖啡斑
resolve(data)
clickPoint = [];//清空
veInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);//
列子汤问停⽌监听移动事件 veInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);//停⽌监听移动事件
}
BLOCK街区}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
关于水viewer.screenSpaceEventHandler.tInputAction(function (movement) {
const newPosition = viewer.camera.dPosition, viewer.scene.globe.ellipsoid);//当前世界坐标笛卡尔积 if (Cesium.defined(newPosition)) {
const movePoint = [...clickPoint, newPosition]
removeEntityByName('moveDrawRectangle')
暴雨洪涝const rectangleHand = {
通告coordinates: new Cesium.CallbackProperty(function () {
const obj = Cesium.Rectangle.fromCartesianArray(movePoint);
return obj;
}, fal),澳门为什么叫妈阁
material: Cesium.Color.fromCssColorString("rgba(198, 27, 27, 0.48)"),
}
name: 'moveDrawRectangle',
rectangle: rectangleHand,
});
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
})
} el {
removeEntityByName('drawRectangle')
removeEntityByName('moveDrawRectangle')
veInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);//停⽌监听移动事件
veInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);//停⽌监听移动事件
}
}
}
使⽤:
const data = await drawRectangle(true)
if (data) {
console.log('矩形经纬度: ', data)
}