标注与撤销

三维标绘、量算、位置拾取、拖拽与模型位置调整。下面是该示例的核心 script 片段,供参考学习;完整可运行代码请进入在线 Demo 或查看仓库源码。

在线运行 Demo 全部示例 查看完整源码

示例代码

import * as Cesium from 'cesium'
import { initCesium } from '@/utils/cesiumPluginsExtends/index'
import { AnnotationStore, AnnotationLayer } from '@/utils/cesiumPluginsExtends/annotation/index'
import {
  defineDrawToolbar,
  bindDrawToolbar
} from '@/utils/cesiumPluginsExtends/web-components/index'

// 演示区域中心(成都附近)
const CENTER = [104.09, 30.65]

export default {
  data () {
    return {
      state: { size: 0, canUndo: false, canRedo: false },
      jsonPreview: ''
    }
  },
  mounted () {
    this.initMap()
  },
  beforeUnmount () {
    if (this.unbindToolbar) this.unbindToolbar()
    if (this.layer) this.layer.destroy()
    this.c_viewer = null
  },
  methods: {
    async addWorldTerrainAsync (viewer) {
      try {
        const terrainProvider = await Cesium.CesiumTerrainProvider.fromIonAssetId(1)
        viewer.terrainProvider = terrainProvider
      } catch (error) {
        console.log(`Failed to add world terrain: ${error}`)
      }
    },
    async initMap () {
      const { viewer } = new initCesium({
        cesiumGlobal: Cesium,
        containerId: 'cesiumContainer',
        viewerConfig: {
          infoBox: false,
          shouldAnimate: true
        },
        extraConfig: {
          depthTest: true,
          AccessToken:
            'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmYzkwZWEwYy1mMmIwLTQwYjctOWJlOC00OWU4ZWU1YTZhOTkiLCJpZCI6MTIxODIsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1NjA0OTUyNDN9.wagvw7GxUjxvHXO6m2jjX5Jh9lN0UyTJhNGEcSm2pgE'
        },
        MapImageryList: [
          {
            type: 'UrlTemplateImageryProvider',
            option: {
              url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
              subdomains: ['0', '1', '2', '3'],
              tilingScheme: new Cesium.WebMercatorTilingScheme()
            }
          }
        ]
      })

      this.c_viewer = viewer
      this.addWorldTerrainAsync(viewer)

      // 数据层 + Cesium 绑定层
      this.store = new AnnotationStore()
      this.layer = new AnnotationLayer({ Cesium, viewer, store: this.store })
      this.store.subscribe((snapshot) => {
        this.state = {
          size: snapshot.annotations.length,
          canUndo: snapshot.canUndo,
          canRedo: snapshot.canRedo
        }
      })

      // 注册并使用 Web Component 工具条
      defineDrawToolbar()
      const toolbar = document.createElement('cesium-draw-toolbar')
      toolbar.setAttribute('modes', 'point,polyline,polygon')
      this.$refs.toolbarHost.appendChild(toolbar)
      this.unbindToolbar = bindDrawToolbar(toolbar, {
        point: () => this.addAnnotation('point'),
        polyline: () => this.addAnnotation('polyline'),
        polygon: () => this.addAnnotation('polygon'),
        cancel: () => {
          toolbar.value = null
        }
      })

      this.flyTo()
      this.seed()
    },
    flyTo () {
      this.c_viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromDegrees(CENTER[0], CENTER[1], 12000),
        orientation: { heading: 0, pitch: Cesium.Math.toRadians(-55), roll: 0 }
      })
    },
    jitter (range = 0.012) {
      return (Math.random() - 0.5) * range
    },
    randomPoint () {
      return [CENTER[0] + this.jitter(), CENTER[1] + this.jitter()]
    },
    addAnnotation (type) {
      const p1 = this.randomPoint()
      if (type === 'point') {
        this.store.add({
          type: 'point',
          positions: [p1],
          style: { color: '#ffcc00', pointSize: 10 }
        })
        return
      }
      if (type === 'polyline') {
        const p2 = this.randomPoint()
        this.store.add({
          type: 'polyline',
          positions: [p1, p2],
          style: { color: '#4fc3f7', lineWidth: 3 }
        })
        return
      }
      // polygon
      const p2 = this.randomPoint()
      const p3 = this.randomPoint()
      this.store.add({
        type: 'polygon',
        positions: [p1, p2, p3],
        style: { color: '#66bb6a', clampToGround: true }
      })
    },
    seed () {
      this.store.add({
        type: 'point',
        positions: [[CENTER[0], CENTER[1]]],
        style: { color: '#ff5252', pointSize: 12 }
      })
      this.store.add({
        type: 'polyline',
        positions: [
          [CENTER[0] - 0.004, CENTER[1] - 0.004],
          [CENTER[0] + 0.004, CENTER[1] + 0.004]
        ],
        style: { color: '#ffd740', lineWidth: 3 }
      })
    },
    undo () {
      this.store.undo()
    },
    redo () {
      this.store.redo()
    },
    exportJson () {
      this.jsonPreview = JSON.stringify(this.store.toJSON(), null, 2)
    },
    loadJson () {
      if (!this.jsonPreview) {
        this.jsonPreview = JSON.stringify(this.store.toJSON(), null, 2)
        return
      }
      try {
        this.store.load(JSON.parse(this.jsonPreview))
      } catch (error) {
        console.warn('JSON 解析失败', error)
      }
    },
    clearAll () {
      this.store.clear()
    }
  }
}

相关 API

安装:npm install cesium_dev_kit cesium