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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
export let graphicLayer // 矢量图层对象
let pathEntity = null
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 32.550222, lng: 117.366824, alt: 2696, heading: 273, pitch: -67 }
  },
  control: {
    clockAnimate: true, // 时钟动画控制(左下角)
    timeline: true // 是否显示时间线控件
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  // 创建矢量数据图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  mars3d.Util.fetchJson({ url: "//data.mars3d.cn/file/apidemo/flypath.json" })
    .then(function (res) {
      initPath(res)
    })
    .catch(function (error) {
      console.log("加载JSON出错", error)
    })
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
// 改变视角  跟踪,上方和侧方
export function viewAircraft() {
  map.trackedEntity = pathEntity.entity
 
  pathEntity.flyToPoint({
    radius: 500, // 距离目标点的距离
    heading: 40,
    pitch: -50,
    duration: 0.01
  })
}
export function viewTopDown() {
  map.trackedEntity = undefined
 
  map.flyToPoint(pathEntity.positionShow, {
    radius: 2000,
    heading: -90,
    pitch: -89
  })
}
export function viewSide() {
  map.trackedEntity = undefined
 
  map.flyToPoint(pathEntity.positionShow, {
    radius: 3000,
    heading: -90,
    pitch: -25
  })
}
 
function initPath(data) {
  const property = new Cesium.SampledPositionProperty()
  property.forwardExtrapolationType = Cesium.ExtrapolationType.HOLD
 
  let start
  let stop
  for (let i = 0, len = data.length; i < len; i++) {
    const item = data[i]
    const lng = Number(item.x.toFixed(6)) // 经度
    const lat = Number(item.y.toFixed(6)) // 纬度
    const height = item.z // 高度
    const time = item.time // 时间
 
    let position = null
    if (lng && lat) {
      position = Cesium.Cartesian3.fromDegrees(lng, lat, height)
    }
    let juliaDate = null
    if (time) {
      juliaDate = Cesium.JulianDate.fromIso8601(time)
    }
    if (position && juliaDate) {
      property.addSample(juliaDate, position)
    }
 
    if (i === 0) {
      start = juliaDate
    } else if (i === len - 1) {
      stop = juliaDate
    }
 
    const primitive = new mars3d.graphic.PointPrimitive({
      position: position,
      style: {
        pixelSize: 4,
        color: "#cccccc"
      },
      popup: "编号:" + item.id + "<br/>时间:" + time
    })
    graphicLayer.addGraphic(primitive)
  }
 
  // 设置时钟属性
  map.clock.startTime = start.clone()
  map.clock.stopTime = stop.clone()
  map.clock.currentTime = start.clone()
  map.clock.clockRange = Cesium.ClockRange.LOOP_STOP
  map.clock.multiplier = 5
 
  if (map.controls.timeline) {
    map.controls.timeline.zoomTo(start, stop)
  }
 
  // 创建path对象
  pathEntity = new mars3d.graphic.PathEntity({
    position: property,
    orientation: new Cesium.VelocityOrientationProperty(property),
    style: {
      resolution: 1,
      leadTime: 0,
      trailTime: 3600,
      color: "#ff0000",
      width: 3
    },
    label: {
      text: "飞机1号",
      font_size: 19,
      font_family: "楷体",
      color: Cesium.Color.AZURE,
      outline: true,
      visibleDepth: false,
      outlineColor: Cesium.Color.BLACK,
      outlineWidth: 2,
      horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
      verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
      pixelOffset: new Cesium.Cartesian2(10, -25) // 偏移量
    },
    model: {
      url: "//data.mars3d.cn/gltf/mars/wrj.glb",
      scale: 0.1,
      minimumPixelSize: 20
    },
    popup: "飞行1号"
  })
  graphicLayer.addGraphic(pathEntity)
 
  // 圆锥追踪体
  const coneTrack = new mars3d.graphic.ConeTrack({
    position: property,
    style: {
      length: 100,
      angle: 12, // 半场角度
      color: "#ff0000",
      opacity: 0.5
    }
  })
  graphicLayer.addGraphic(coneTrack)
}