Cesium使用鼠标动态绘制多边形(附带绘制帮助提示语)

实现鼠标动态绘制多边形的类PolygonDraw

实现思路是:动态绘制多边形实际与动态绘制线区别不大,唯一的区别就是在构建形状时一个是构建线,一个是构建面,这里我在绘制多边形时额外在多边形外围加了一圈线以此来代表多边形的边框,因为Cesium自带的outLine属性设置宽度是无效的。

代码如下:

import CesiumByZh from "../CesiumByZh";
import GlobalListener from "../GlobalListener";


export default class PolygonDraw {
    protected positions: any[] = [];
    protected position: any = CesiumByZh.Cesium.Cartesian3.fromDegrees(103, 30, 100);
    protected dragIconLight: string = "./Images/circle_red.png";
    protected shapeEntity: any;
    protected okHandler: any;
    protected cancelHandler: any;
    protected handler: any;
    protected modifyHandler: any;
    protected mousePosition: any[] = [];
    protected result: any;
    public pointEntityArray: any[] = [];
    public canvas = CesiumByZh.viewer.scene.canvas;
    public camera = CesiumByZh.viewer.scene.camera;
    public ellipsoid = CesiumByZh.viewer.scene.globe.ellipsoid;
    public viewer = CesiumByZh.viewer;
    public scene = CesiumByZh.viewer.scene;
    lineColor: string = '#002fff';
    fillColor: string = '#00ff36';
    lineOpacity: number = 50;
    fillOpacity: number = 50;
    isGround: boolean = true;
    lineWidth: number = 2;

    beginDraw(drawHandler: any, okHandler: any, cancelHandler: any) {
        let that = this;
        let floatingPoint: any = null;

        function clicka(e: any) {
            if (e.button == 0) {
                let mouseposition = {
                    x: e.pageX,
                    y: e.pageY,
                };
                that.mousePosition.push(mouseposition)
            }
        }

        this.handler = drawHandler;
        this.handler.setInputAction((event: any) => {
            let position = event.position;
            if (!CesiumByZh.Cesium.defined(position)) {
                return;
            }
            let ray = this.camera.getPickRay(position);
            if (!CesiumByZh.Cesium.defined(ray)) {
                return;
            }
            let cartesian = this.scene.globe.pick(ray, this.scene);
            if (!CesiumByZh.Cesium.defined(cartesian)) {
                return;
            }
            let num = this.positions.length;
            if (num == 0) {
                this.positions.push(cartesian);
                this.createTemporaryShape();
            }
            this.positions.push(cartesian);
            floatingPoint = this._createPoint(cartesian);
            this.pointEntityArray.push(floatingPoint);
        }, CesiumByZh.Cesium.ScreenSpaceEventType.LEFT_CLICK);
        this.handler.setInputAction((event: any) => {
            let position = event.endPosition;
            if (!CesiumByZh.Cesium.defined(position)) {
                return;
            }
            let ray = this.camera.getPickRay(position);
            if (!CesiumByZh.Cesium.defined(ray)) {
                return;
            }
            let cartesian = this.scene.globe.pick(ray, this.scene);
            if (!CesiumByZh.Cesium.defined(cartesian)) {
                return;
            }
            if (this.positions.length == 0) {
                GlobalListener.getInstance().runTipsCallback(['单击开始绘制,右键取消']);
                return
            } else if (this.positions.length < 3) {
                GlobalListener.getInstance().runTipsCallback(['单击增加点,右键删除点']);
            } else {
                GlobalListener.getInstance().runTipsCallback(['单击增加点,右键删除点', '双击完成绘制']);
            }
            this.positions.pop();
            this.positions.push(cartesian);
        }, CesiumByZh.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        this.handler.setInputAction((movement: any) => {
            if (this.positions.length == 0) {
                //重新开始绘制
            } else {
                if (this.positions.length == 2) {
                    this.positions = [];
                } else {
                    this.positions.splice(this.positions.length - 2, 1);
                }
                CesiumByZh.viewer.entities.remove(this.pointEntityArray[this.pointEntityArray.length - 1]);
                this.pointEntityArray.pop();
            }
        }, CesiumByZh.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
        this.handler.setInputAction((movement: any) => {
            if (this.positions.length < 4) {
                return;
            }
            this.positions.pop();
            document.removeEventListener('mousemove', clicka);
        }, CesiumByZh.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
        document.addEventListener('click', clicka);
        this.okHandler = okHandler;
        this.cancelHandler = cancelHandler;
    }

    _createPoint(cartesian: any, oid: number = 0) {
        let point = CesiumByZh.viewer.entities.add({
            temporary: true,//是否为绘制临时使用
            oid: oid,
            canSelect: true,
            position: cartesian,
            billboard: {
                image: this.dragIconLight,
                eyeOffset: new CesiumByZh.Cesium.ConstantProperty(new CesiumByZh.Cesium.Cartesian3(0, 0, -500)),
                heightReference: CesiumByZh.Cesium.HeightReference.CLAMP_TO_GROUND
            }
        });
        return point;
    }

    //创建临时绘制用的实体
    createTemporaryShape() {
        let outlineDynamicPositions = new CesiumByZh.Cesium.CallbackProperty(() => {
            if (this.positions.length > 1) {
                // @ts-ignore
                let arr: any = [].concat(this.positions);
                let first = this.positions[0];
                arr.push(first);
                return arr;
            } else {
                return null;
            }
        }, false);
        this.shapeEntity = CesiumByZh.viewer.entities.add({
            polygon: {
                hierarchy: new CesiumByZh.Cesium.CallbackProperty(() => {
                    return new CesiumByZh.Cesium.PolygonHierarchy(this.positions)
                }, false),
                material: CesiumByZh.Cesium.Color.fromCssColorString(this.fillColor).withAlpha(this.fillOpacity / 100),
                height: undefined
            },
            polyline: {
                positions: outlineDynamicPositions,
                clampToGround: this.isGround,
                width: this.lineWidth,
                material: CesiumByZh.Cesium.Color.fromCssColorString(this.lineColor).withAlpha(this.lineOpacity / 100),
            }
        });
    }

}
抱歉,作为AI言模型,我不能提供代码。但我可以给你一些思路和建议。要在cesium使用鼠标动态绘制曲线,你需要以下步骤: 1. 监听鼠标点击事件,获取鼠标在地球上的位置。 2. 将鼠标点击的位置转换成笛卡尔坐标系。 3. 将笛卡尔坐标系转换成经纬度坐标系。 4. 将经纬度坐标系转换成cesium中的Cartesian3坐标系。 5. 将Cartesian3坐标系坐标添加到cesium的Entity对象中,以便可以动态绘制曲线。 6. 使用cesium的PolylineGraphics对象设置曲线的样式、颜色等属性。 以下是一个示例代码片段,仅供参考: ```javascript var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); var linePositions = []; handler.setInputAction(function (click) { // 获取鼠标点击位置的笛卡尔坐标系 var cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid); if (cartesian) { // 将笛卡尔坐标系转换成经纬度坐标系 var cartographic = Cesium.Cartographic.fromCartesian(cartesian); // 将经纬度坐标系转换成cesium的Cartesian3坐标系 var position = Cesium.Cartesian3.fromDegrees(cartographic.longitude, cartographic.latitude, cartographic.height); linePositions.push(position); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); var entity = viewer.entities.add({ polyline: { positions: linePositions, width: 5, material: Cesium.Color.RED } }); ``` 这段代码监听了鼠标左键点击事件,获取鼠标点击位置的坐标系,并将其添加到linePositions数组中。然后使用这个数组创建了一个PolylineGraphics对象,设置了线条的颜色和宽度,并将其添加到了cesium的Entity对象中,以便可以动态绘制曲线。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sannianerban12138

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

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

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

打赏作者

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

抵扣说明:

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

余额充值