cesium 移动线条上的某个点

cesium 鼠标拖动点移动图形

效果图:

GlobalMap.prototype.movePoint = function(p,entity){
    //p 是点击点的位置  entity是点击时的点
    //p -> that.Viewer.entities.getById(entitiesId)._position._value    
    //entity -> that.Viewer.entities.getById(entitiesId)
    let that = this;
    //用矩阵计算移动后的位置
    function getPoint(x=0,y=0,z=0,point) {
        // x、y、z为三个方向要移动的距离,point是要移动的点
        let m =Cesium.Matrix4.fromArray([
            1.0,0,0,0,
            0,1.0,0,0,
            0,0,1.0,0,
            x,y,z,1
        ]);
        return  Cesium.Matrix4.multiplyByPoint(m,point,new Cesium.Cartesian3())
    }
    var startpos = p;
    //计算箭头的终点
    var result_x = getPoint(200000,0,0, p); //x 正方向
    var result_y = getPoint(0,200000,0, p);
    var result_z = getPoint(0,0,200000, p);
    var result_xN = getPoint(-200000,0,0, p); //x 负方向
    var result_yN = getPoint(0,-200000,0, p);
    var result_zN = getPoint(0,0,-200000, p);
    var direction;
    //绘制箭头
    arrow_x = this.Viewer.entities.add({
        polyline: {
            positions: new Cesium.CallbackProperty(()=>{
                return [startpos, result_x]
            },false),
            clampToGround: true,
            width: 20,
            followSurface: false,
            material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.GREEN)
        },
        name:'x+'
    });
    arrow_y = this.Viewer.entities.add({
        polyline: {
            positions: new Cesium.CallbackProperty(()=>{
                return [startpos, result_y]
            },false),
            clampToGround: true,
            width: 20,
            followSurface: false,
            material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.RED)
        },
        name:'y+'
    });
    arrow_z = this.Viewer.entities.add({
        polyline: {
            positions: new Cesium.CallbackProperty(()=>{
                return [startpos, result_z]
            },false),
            clampToGround: true,
            width: 20,
            followSurface: false,
            material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.BLUE)
        },
        name:'z+'
    });
    arrow_xN = this.Viewer.entities.add({
        polyline: {
            positions: new Cesium.CallbackProperty(()=>{
                return [startpos, result_xN]
            },false),
            clampToGround: true,
            width: 20,
            followSurface: false,
            material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.GREEN)
        },
        name:'x-'
    });
    arrow_yN = this.Viewer.entities.add({
        polyline: {
            positions: new Cesium.CallbackProperty(()=>{
                return [startpos, result_yN]
            },false),
            clampToGround: true,
            width: 20,
            followSurface: false,
            material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.RED)
        },
        name:'y-'
    });
    arrow_zN = this.Viewer.entities.add({
        polyline: {
            positions: new Cesium.CallbackProperty(()=>{
                return [startpos, result_zN]
            },false),
            clampToGround: true,
            width: 20,
            followSurface: false,
            material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.BLUE)
        },
        name:'z-'
    });


    function move(x=0,y=0,z=0){
        entity.position._value = startpos = getPoint(x,y,z,startpos);
            old_pos.splice(index,1,startpos);
        if(entityType == 'polyline'){
            that.Viewer.entities.getById(graphId).polyline.positions = new Cesium.CallbackProperty(()=>{
                return old_pos; //更新线条位置
            }, false)
        }else {
            that.Viewer.entities.getById(graphId).polygon.hierarchy = new Cesium.CallbackProperty(()=>{
                return new Cesium.PolygonHierarchy(old_pos);
            }, false)
        }
        // ---entity.position._value = startpos = Cesium.Matrix4.multiplyByPoint(getM(x,y,z),startpos,new Cesium.Cartesian3())
        //更新箭头位置
        result_x = getPoint(x,y,z,result_x);
        result_y = getPoint(x,y,z,result_y);
        result_z = getPoint(x,y,z,result_z);
        result_xN = getPoint(x,y,z,result_xN);
        result_yN = getPoint(x,y,z,result_yN);
        result_zN = getPoint(x,y,z,result_zN);

    }
    var leftDownFlag = false;
    var pointDraged;
    var index;
    var entityType;
    Handler = new Cesium.ScreenSpaceEventHandler(this.Viewer.scene.canvas);
    Handler.setInputAction(function (movement) {
        pointDraged = that.Viewer.scene.pick(movement.position);//选取当前的entity
        leftDownFlag = true;
        if (pointDraged) {
        //记录按下去的坐标
        //     startPoint = that.Viewer.scene.pickPosition(movement.position);
            that.Viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机

            if(pointDraged.id._polyline && pointDraged.id._polyline._material){
                //判断点的是哪个方向的箭头
                if(pointDraged.id._name){
                    direction = pointDraged.id._name
                }
                //根据点id找图形id
                for(let key in dots){
                    dots[key].forEach(item =>{
                        if(item._id == entity._id){
                            graphId = key
                            return;
                        }
                    })
                }

                if(that.Viewer.entities.getById(graphId).polyline){//如果移动的是线条
                    //old位置
                    if(!old_pos){
                        old_pos = that.Viewer.entities.getById(graphId)._polyline._positions._value
                    }
                    entityType = 'polyline'
                    //记录按下去的点index
                    old_pos.forEach((item,i)=>{
                        var dis = Cesium.Cartesian3.distance (item, startpos)
                        if(dis<3000){
                            index = i  //得到应该移动的点的index
                        }
                    })

                }else if(that.Viewer.entities.getById(graphId).polygon){
                    //old位置
                    if(!old_pos){
                        old_pos = that.Viewer.entities.getById(graphId)._polygon._hierarchy._value.positions
                    }
                    entityType = 'polygon'
                    //记录按下去的点index
                    old_pos.forEach((item,i)=>{
                        var dis = Cesium.Cartesian3.distance (item, startpos)
                        if(dis<3000){
                            index = i
                        }
                    })
                    // coordinates
                }else if(that.Viewer.entities.getById(graphId).rectangle){
                    if(!old_pos){
                        old_pos = that.Viewer.entities.getById(graphId)._polygon._hierarchy._value.positions
                    }
                }


            }

        }
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
    Handler.setInputAction(function (movement) {
        if (leftDownFlag === true && pointDraged != null && direction  && entityType) {
            let x,y,z;
            x = y =z =0;
            let d = direction.substring(0,1);
            if(direction.includes('+')){
                d == 'x' ? x = 2000 : d == 'y'? y = 2000 : z = 2000
            }else {
                d == 'x' ? x = -2000 : d == 'y'? y = -2000 : z = -2000
            }
            move(x,y,z)
        }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    Handler.setInputAction(function (movement) {

        leftDownFlag = false;
        pointDraged=null;
        index = null;
        entityType = null;
        direction = null;
        that.Viewer.scene.screenSpaceCameraController.enableRotate = true;//解锁相机

    }, Cesium.ScreenSpaceEventType.LEFT_UP);
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Cesium地图上画,你可以使用`viewer.entities.add`方法,并在其中定义一个具有`position`和`point`属性的对象。`position`属性指定的位置,可以使用`Cesium.Cartesian3.fromDegrees`方法将经纬度转换为笛卡尔坐标。`point`属性定义的样式,包括颜色和大小等。以下是一个示例代码: ```javascript viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(经度, 纬度), point: { color: Cesium.Color.YELLOW, pixelSize: 10 } }); ``` 请注意,你需要将`经度`和`纬度`替换为实际的经度和纬度值。这将在地图上添加一个黄色的,大小为10像素。你可以根据需要调整颜色和大小等属性。 #### 引用[.reference_title] - *1* *3* [Cesium三维地球上添加、线、面、文字、图标(图片)、模型等标绘](https://blog.csdn.net/qq_17627195/article/details/109237689)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [cesium拾取当前地图击位置坐标,并在添加到图上显示](https://blog.csdn.net/weixin_45782925/article/details/123364366)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值