Cesium 实现Entity的拖拽

引入有时候我们需要对我们在cesium上绘制的点,线,面等对象微调位置。如果可以直接拖拽对象到指定的位置最方便不过了。

思路

1.覆写鼠标事件,其中:leftDownAction(),leftUpAction(),mouseMoveAction()是自定义的函数,有vm是因为使用了Vue。

 this.viewer.screenSpaceEventHandler
.setInputAction(vm.leftDownAction, Cesium.ScreenSpaceEventType.LEFT_DOWN);
      this.viewer.screenSpaceEventHandler
.setInputAction(vm.leftUpAction, Cesium.ScreenSpaceEventType.LEFT_UP);
      this.viewer.screenSpaceEventHandler
.setInputAction(vm.mouseMoveAction, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

2.定义一个全局变量:var  isLeftDown=false;来标识是否鼠标左键已经按下,定义一个全局变量:var  pointDraged;来表示拖拽的对象。

3.下面详细说下三个自定义函数:

  • leftDownAction():
    leftDownAction(e) {
            console.log("左键按下");
            pointDraged = this.viewer.scene.pick(e.position);//选取当前的entity
            leftDownFlag = true;
            if (pointDraged) {
              this.viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
            }
          }

     

  • leftUpAction():
     leftUpAction(e) {
            console.log("左键抬起");
            leftDownFlag = false;
            pointDraged=null;
            this.viewer.scene.screenSpaceCameraController.enableRotate = true;//解锁相机
          }
  • mouseMoveAction():
    mouseMoveAction(e) {
            if (leftDownFlag === true && pointDraged != null) {
              console.log("鼠标移动");
              let ray = this.viewer.camera.getPickRay(e.endPosition);
              let cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
                console.log(cartesian);
                pointDraged .position=cartesian;//此处根据具体entity来处理,也可能是pointDraged.id.position=cartesian;
              }

     

 

4.这样就可以了,上面的代码只实现了基本功能,请结合你的场景做适当调整,如有不正确的地方,欢迎提出改正。

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大大大大大碗面

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

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

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

打赏作者

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

抵扣说明:

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

余额充值