fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 26.520735, lng: 99.609792, alt: 23891502.7, heading: 93.3, pitch: -80.8, roll: 266.7 },
    clock: {
      multiplier: 200 // 速度
    }
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  // 按shift键+鼠标左键 拖拽 地球到合适区域,通过下面代码获取视角参数,拷贝到mapOptions的center参数中。
  const center = JSON.stringify(map.getCameraView({ simplify: false }))
  console.log("center视角为:", center)
 
  startRotate()
 
  getGeojsonStart()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
let previousTime
 
export function startRotate() {
  stopRotate()
  previousTime = map.clock.currentTime.secondsOfDay
  map.on(mars3d.EventType.clockTick, map_onClockTick)
}
export function stopRotate() {
  map.off(mars3d.EventType.clockTick, map_onClockTick)
}
// 地球旋转
function map_onClockTick() {
  const spinRate = 1
 
  const currentTime = map.clock.currentTime.secondsOfDay
  const delta = (currentTime - previousTime) / 1000
  previousTime = currentTime
  map.scene.camera.rotate(Cesium.Cartesian3.UNIT_Z, -spinRate * delta)
}
 
// 加载 演示数据
export function getGeojsonStart() {
  startRotate()
  // 获取演示数据并加载
  mars3d.Util.fetchJson({ url: "//data.mars3d.cn/file/geojson/areas/100000_full.json" })
    .then(function (json) {
      addDemoGraphics(json)
    })
    .catch(function (error) {
      console.log("加载JSON出错", error)
    })
}
 
function addDemoGraphics(geojson) {
  const center = Cesium.Cartesian3.fromDegrees(117.203932, 31.856794, 31.8)
  // 公司位置 矢量对象标记
  const lightCone = new mars3d.graphic.LightCone({
    position: center,
    style: {
      color: "rgba(0,255,255,0.9)",
      radius: 80000, // 底部半径
      height: 1000000 // 椎体高度
    },
    show: true
  })
  map.graphicLayer.addGraphic(lightCone)
 
  const arr = mars3d.Util.geoJsonToGraphics(geojson) // 解析geojson
  const lineMaterial = mars3d.MaterialUtil.createMaterial(mars3d.MaterialType.LineFlow, {
    image: "img/textures/line-color-yellow.png",
    color: new Cesium.Color(255 / 255, 201 / 255, 38 / 255, 0.5),
    speed: 9
  })
  for (let i = 0; i < arr.length; i++) {
    const item = arr[i].attr
    if (item.name) {
      const thisPoint = Cesium.Cartesian3.fromDegrees(item.center[0], item.center[1])
      const positions = mars3d.PolyUtil.getLinkedPointList(center, thisPoint, 40000, 100) // 计算曲线点
      const primitive = new mars3d.graphic.PolylinePrimitive({
        positions: positions,
        style: {
          width: 2,
          material: lineMaterial // 动画线材质
        },
        attr: item
      })
      primitive.bindTooltip(`合肥 - ${item.name}`)
      map.graphicLayer.addGraphic(primitive)
    }
  }
}