Cesium.js点线面绘制

Cesium.js在三维GIS中十分的流行,点、线、面的绘制也是GIS开发中经常用到的基础操作。最近在Ceisum.js三维开发时,也是遇到了点、线、面的绘制功能开发,正好这里记录分享一下。

效果

效果

核心代码

  • 鼠标坐标获取

注意绘制功能要使用的是三维坐标,最后的点线面要与地形吻合。

let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    handler.setInputAction((click)=>{
      // click.position 二维坐标
      console.log('左键单击事件:',click.position);
      console.log('左键单击事件:',click);
      let ray = viewer.camera.getPickRay(click.position);
      // cartesian 三维坐标
      let cartesian = viewer.scene.globe.pick(ray, viewer.scene);
      points.push(cartesian)
      
    },Cesium.ScreenSpaceEventType.LEFT_CLICK);
  • 点绘制
 addPoints(position){
      let entity = new Cesium.Entity({
        name: 'test',
        show: true,
        position: position,
        point: {
          show: true,
          pixelSize: 10,
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
          color: Cesium.Color.RED,
          outlineColor: Cesium.Color.YELLOW,
          outlineWidth: 3,
          disableDepthTestDistance: Number.POSITIVE_INFINITY,
          distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 20000)
        },
      });
      window.viewer.entities.add(entity);
    },
  • 线绘制
drawLine(points){
      if(points.length>=2){
        window.viewer.entities.add({
          name: 'line',
          polyline: {
            positions:points,
            width: 5,
            material: Cesium.Color.RED,
            clampToGround: true
          }
        })
      }
    },
  • 面绘制
 drawPolygon(points){
      if(points.length>2){
        window.viewer.entities.add({
          name: 'polygon',
          polygon: {
            hierarchy:points,
            material: Cesium.Color.GREEN,
          }
        })
      }
    },

全部代码

<template>
  <div id="cesiumContainer">

  </div>
</template>

<script>
import * as Cesium from 'cesium';

export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },

  setup() {
    window.CESIUM_BASE_URL = '/cesium/';
    Cesium.Ion.defaultAccessToken = '你的token';
  },

  mounted() {
    // "cesiumContainer"是需要渲染地图的dom的id.
    const viewer = new Cesium.Viewer('cesiumContainer', {
      terrainProvider: Cesium.createWorldTerrain()
    });


    viewer.camera.setView({
      // Cesium的坐标是以地心为原点,一向指向南美洲,一向指向亚洲,一向指向北极州
      // fromDegrees(lng,lat,height)方法,将经纬度和高程转换为世界坐标
      destination: Cesium.Cartesian3.fromDegrees(111.6265869140625,35.016500995886005,2000),
      orientation: {
        heading : Cesium.Math.toRadians(0.0),
        pitch : Cesium.Math.toRadians(-75),
      }
    })
    viewer.zoomTo(viewer);
    let points=[]

    window.viewer = viewer
    let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    handler.setInputAction((click)=>{
      // click.position 二维坐标
      console.log('左键单击事件:',click.position);
      console.log('左键单击事件:',click);
      let ray = viewer.camera.getPickRay(click.position);
      // cartesian 三维坐标
      let cartesian = viewer.scene.globe.pick(ray, viewer.scene);
      points.push(cartesian)
      this.addPoints(cartesian)
      this.drawLine(points)
    },Cesium.ScreenSpaceEventType.LEFT_CLICK);

    handler.setInputAction((click)=>{
      // click.position 二维坐标
      // 结束
      this.closeLine(points)
      this.drawPolygon(points)
      
    },Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    


  },
  methods:{
    //绘制面
    drawPolygon(points){
      if(points.length>=2){
        window.viewer.entities.add({
          name: 'polygon',
          polygon: {
            hierarchy:points,
            material: Cesium.Color.GREEN,
          }
        })
      }
    },
    addPoints(position){
      let entity = new Cesium.Entity({
        name: 'test',
        show: true,
        position: position,
        point: {
          show: true,
          pixelSize: 10,
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
          color: Cesium.Color.RED,
          outlineColor: Cesium.Color.YELLOW,
          outlineWidth: 3,
          disableDepthTestDistance: Number.POSITIVE_INFINITY,
          distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 20000)
        },
      });
      viewer.entities.add(entity);
    },
    drawLine(points){
      if(points.length>=2){
        window.viewer.entities.add({
          name: 'line',
          polyline: {
            positions:[points[points.length-2],points[points.length-1]],
            width: 5,
            material: Cesium.Color.RED,
            clampToGround: true
          }
        })
      }
    },
    closeLine(points){
      if(points.length>=2){
        window.viewer.entities.add({
          name: 'line',
          polyline: {
            positions:[points[points.length-1],points[0]],
            width: 5,
            material: Cesium.Color.RED,
            clampToGround: true
          }
        })
      }
    },

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#cesiumContainer {
  width: 100%;
  height: 100%;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GIS开发者

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

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

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

打赏作者

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

抵扣说明:

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

余额充值