cesium绘制矩形并返回矩形经纬度及面积

使用turf.js库计算面积

 

  // 绘制矩形
    function drawRectangle(whetherStart) {
        if (viewer) {
            // drawPoint(false)
            if (whetherStart) {
                let clickPoint = [];
                removeEntityByName('drawRectangle')
                return new Promise((resolve, reject) => {
                    // 鼠标左键绘制
                    viewer.screenSpaceEventHandler.setInputAction(function (movement) {
                        const earthPosition = viewer.camera.pickEllipsoid(movement.position, viewer.scene.globe.ellipsoid);//当前世界坐标笛卡尔积
                        if (Cesium.defined(earthPosition)) {
                            // 判断现在是第几个点
                            if (clickPoint.length === 0) {//还没有点,添加第一个点
                                clickPoint.push(earthPosition)
                            } else {//绘制矩形
                                clickPoint.push(earthPosition)

                                // 绘制矩形
                                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
                                ]]);
                                // 面积(m²)
                                var area = turf.area(polygon);
                                const data = {
                                    leftTop: leftTop,
                                    leftBottom: leftBottom,
                                    rightTop: rightTop,
                                    rightBottom: rightBottom,
                                    area: area,//面积(m²)
                                }
                                resolve(data)
                                clickPoint = [];//清空
                                viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);//停止监听移动事件
                                viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);//停止监听移动事件
                            }
                        }
                    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

                    viewer.screenSpaceEventHandler.setInputAction(function (movement) {
                        const newPosition = viewer.camera.pickEllipsoid(movement.endPosition, 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;
                                }, false),
                                material: Cesium.Color.fromCssColorString("rgba(198, 27, 27, 0.48)"),
                            }
                            viewer.entities.add({
                                name: 'moveDrawRectangle',
                                rectangle: rectangleHand,
                            });
                        }
                    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
                })
            } else {
                removeEntityByName('drawRectangle')
                removeEntityByName('moveDrawRectangle')
                viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);//停止监听移动事件
                viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);//停止监听移动事件
            }
        }
    }

使用:

 const data = await drawRectangle(true)
 if (data) {
    console.log('矩形经纬度: ', data)
 }

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现cesium动态绘制矩形,可以使用Cesium的Primitive API和MouseEvents。具体步骤如下: 1. 创建矩形entity并添加到场景中。 ``` var entity = viewer.entities.add({ rectangle: { coordinates: Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0), material: Cesium.Color.RED.withAlpha(0.5), outline: true, outlineColor: Cesium.Color.BLACK } }); ``` 2. 获取鼠标左键按下和移动事件,并更新矩形的坐标。 ``` var startPosition; var mouseHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); mouseHandler.setInputAction(function(movement) { startPosition = movement.position; }, Cesium.ScreenSpaceEventType.LEFT_DOWN); mouseHandler.setInputAction(function(movement) { if (Cesium.defined(startPosition)) { var endPosition = movement.position; var startCartesian = viewer.scene.camera.pickEllipsoid(startPosition, viewer.scene.globe.ellipsoid); var endCartesian = viewer.scene.camera.pickEllipsoid(endPosition, viewer.scene.globe.ellipsoid); if (Cesium.defined(startCartesian) && Cesium.defined(endCartesian)) { var startCartographic = Cesium.Cartographic.fromCartesian(startCartesian); var endCartographic = Cesium.Cartographic.fromCartesian(endCartesian); var west = Math.min(startCartographic.longitude, endCartographic.longitude); var east = Math.max(startCartographic.longitude, endCartographic.longitude); var south = Math.min(startCartographic.latitude, endCartographic.latitude); var north = Math.max(startCartographic.latitude, endCartographic.latitude); entity.rectangle.coordinates = Cesium.Rectangle.fromDegrees(west, south, east, north); } } }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); ``` 3. 获取鼠标左键松开事件,结束矩形绘制。 ``` mouseHandler.setInputAction(function(movement) { startPosition = undefined; }, Cesium.ScreenSpaceEventType.LEFT_UP); ``` 这样,当用户按下鼠标左键并移动时,就可以动态绘制矩形

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钢镚是个小屁精

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值