利用Cesium实现在earth上面画点、画线、画面、和圆

今日目的:实现在earth上面画点、画线、画面、和圆

步骤1:
还是按照前几天的项目格式来,现在EarthComp.vue里面写一点东西,先写个按钮吧

<div ref="earthContainer" style="width: 100%; heitht: 100%">           
      <button @click="point">画点</button>
      <button @click="line">画线</button>
      <button @click="polygon">画面</button>
      <button @click="circle">画圆</button>
</div>

先定义了四个按钮,有按钮就需要methods,然后在写上methods

 //这个methods就是按钮实现的功能
 methods: {    
    point(){
      drawingMode = "point";
    },
    line() {
      drawingMode = "line";
    },
    polygon() {
      drawingMode = "polygon";
    },
    circle() {
      drawingMode = "circle";
    },
  },

第二步骤:一下步骤在mounted()方法中完成
上面完成了基本的打框架,接下来要写一下中间的内容
画点

 //绘制点
    function createPoint(worldPosition) {
      var point = earth.czm.viewer.entities.add({
        position: worldPosition,
        point: {
          color: Cesium.Color.WHITE,
          pixelSize: 5,
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        },
      });
      return point;
    }

主要方法:

//画线、面、圆方法
    drawingMode = "circle";
    function drawShape(positionData) {
      var shape;
      //画线
      if (drawingMode === "line") {
        shape = earth.czm.viewer.entities.add({
          polyline: {
            positions: positionData,
            clampToGround: true,
            width: 3,
          },
        });
        //画面
      } else if (drawingMode === "polygon") {
        shape = earth.czm.viewer.entities.add({
          polygon: {
            hierarchy: positionData,
            material: new Cesium.ColorMaterialProperty(
              Cesium.Color.WHITE.withAlpha(0.7)
            ),
          },
        });
        //画圆
      } else if (drawingMode === "circle") {
        //当positionData为数组时绘制最终图,如果为function则绘制动态图
        var value =
          typeof positionData.getValue === "function"
            ? positionData.getValue(0)
            : positionData;
        //var start = activeShapePoints[0];
        //var end = activeShapePoints[activeShapePoints.length - 1];
        //var r = Math.sqrt(Math.pow(start.x - end.x, 2) + Math.pow(start.y - end.y, 2));
        //r = r ? r : r + 1;
        shape = earth.czm.viewer.entities.add({
          position: activeShapePoints[0],
          name: "Blue translucent, rotated, and extruded ellipse with outline",
          type: "Selection tool",
          ellipse: {
            semiMinorAxis: new Cesium.CallbackProperty(function () {
              //半径 两点间距离
              var r = Math.sqrt(
                Math.pow(value[0].x - value[value.length - 1].x, 2) +
                  Math.pow(value[0].y - value[value.length - 1].y, 2)
              );
              return r ? r : r + 1;
            }, false),
            semiMajorAxis: new Cesium.CallbackProperty(function () {
              var r = Math.sqrt(
                Math.pow(value[0].x - value[value.length - 1].x, 2) +
                  Math.pow(value[0].y - value[value.length - 1].y, 2)
              );
              return r ? r : r + 1;
            }, false),
            material: Cesium.Color.BLUE.withAlpha(0.5),
            outline: true,
          },
        });
      }
      return shape;
    }

写一个鼠标点击事件

//鼠标点击事件
    var activeShapePoints = [];
    var activeShape;
    var floatingPoint;
    var handler = new Cesium.ScreenSpaceEventHandler(earth.czm.viewer.canvas);
    handler.setInputAction(function (event) {
      // We use `viewer.scene.pickPosition` here instead of `viewer.camera.pickEllipsoid` so that
      // we get the correct point when mousing over terrain.
      var earthPosition = earth.czm.viewer.scene.pickPosition(event.position);
      // `earthPosition` will be undefined if our mouse is not over the globe.
      if (Cesium.defined(earthPosition)) {
        if (activeShapePoints.length === 0) {
          floatingPoint = createPoint(earthPosition);
          activeShapePoints.push(earthPosition);
          var dynamicPositions = new Cesium.CallbackProperty(function () {
            if (drawingMode === "polygon") {
              return new Cesium.PolygonHierarchy(activeShapePoints);
            }
            return activeShapePoints;
          }, false);
          activeShape = drawShape(dynamicPositions);
        }
        activeShapePoints.push(earthPosition);
        createPoint(earthPosition);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

在写一个鼠标移动事件

//定义几个属性,用不到可以删除
    var activeShapePoints = [];
    var activeShape;
    var floatingPoint;
    var handler = new Cesium.ScreenSpaceEventHandler(earth.czm.viewer.canvas);
  //鼠标的移动事件
    handler.setInputAction(function (event) {
      if (Cesium.defined(floatingPoint)) {
        var newPosition = earth.czm.viewer.scene.pickPosition(
          event.endPosition
        );
        if (Cesium.defined(newPosition)) {
          floatingPoint.position.setValue(newPosition);
          activeShapePoints.pop();
          activeShapePoints.push(newPosition);
        }
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

重新绘制形状,使它不是动态的,并删除动态形状。

function terminateShape() {
      activeShapePoints.pop();
      drawShape(activeShapePoints);
      earth.czm.viewer.entities.remove(floatingPoint);
      earth.czm.viewer.entities.remove(activeShape);
      floatingPoint = undefined;
      activeShape = undefined;
      activeShapePoints = [];
    }

鼠标右击事件,这句话是右键取消

handler.setInputAction(function (event) {
      terminateShape();
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    
    earth.czm.viewer.scene.globe.depthTestAgainstTerrain = true;
    const objConfig = {};
    earth.sceneTree.root.children.push(scene);
    //console.log(earth.sceneTree.$refs);
    //console.log(scene.json);
    //将earth抛出去
    this._earth = earth;
    //仅为调试方便用
    window.earth = earth;
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Vue和Cesium库来实现画线功能。首先,确保你已经安装了Cesium库并在你的Vue项目中引入了它。然后,按照以下步骤进行操作: 1. 在你的Vue组件中,引入Cesium库: ```javascript import * as Cesium from 'cesium' ``` 2. 在`mounted`生命周期钩子函数中创建一个Cesium Viewer实例: ```javascript mounted() { this.viewer = new Cesium.Viewer('cesiumContainer', { terrainProvider: Cesium.createWorldTerrain(), }) } ``` 这将在Vue组件的模板中创建一个具有指定ID的div元素作为Cesium Viewer的容器。 3. 在`methods`部分定义绘制线条的方法: ```javascript methods: { drawLine() { const viewer = this.viewer const lineCoordinates = [ Cesium.Cartesian3.fromDegrees(lon1, lat1, height1), Cesium.Cartesian3.fromDegrees(lon2, lat2, height2), // 添加更多坐标点... ] const line = viewer.entities.add({ polyline: { positions: lineCoordinates, width: 5, material: Cesium.Color.RED, }, }) viewer.zoomTo(line) // 可选:缩放到绘制的线条 }, } ``` 在`drawLine()`方法中,你可以使用`Cesium.Cartesian3.fromDegrees()`方法定义每个点的经纬度坐标,并将它们存储在`lineCoordinates`数组中。然后,通过调用`viewer.entities.add()`方法并传入相关参数创建一个多段线实体。 4. 在模板中添加一个按钮,触发`drawLine()`方法: ```html <template> <div> <div id="cesiumContainer"></div> <button @click="drawLine">绘制线条</button> </div> </template> ``` 这样,当你点击"绘制线条"按钮时,`drawLine()`方法将被触发,从而在Cesium Viewer中绘制指定的线条。 请注意,上述代码只是一个示例,你需要根据你的具体需求进行适当的修改。你可以添加更多的坐标点来绘制复杂的线条,并且还可以为线条设置不同的样式和材质。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值