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
// AQI来源:https://aqicn.org
 
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
export let graphicLayer // 矢量图层对象
 
let imgData = null
let lastExtent = null
let bWorking = false
let currentData
 
let startTimestamp, endTimestamp
let timeout = 1000
let worker
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 25.251743, lng: 107.045599, alt: 553192, heading: 0, pitch: -51 }
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
 
  imgData = getImageData()
 
  // 创建Graphic图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  // 在layer上绑定Popup单击弹窗
  graphicLayer.bindPopup(function (event, callback) {
    const item = event.graphic.attr
    if (!item) {
      return false
    }
    return mars3d.Util.getTemplateHtml({
      title: "AQI",
      template: [
        { field: "city", name: "城市" },
        { field: "utime", name: "时间" },
        { field: "aqi", name: "AQI" },
        { field: "level", name: "质量" },
        { field: "influence", name: "影响" },
        { field: "suggestion", name: "建议" }
      ],
      attr: item
    })
  })
 
  map.on(mars3d.EventType.cameraChanged, onMap_cameraChanged)
  onMap_cameraChanged()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
function onMap_cameraChanged() {
  endTimestamp = new Date().getTime()
  if (bWorking === false) {
    const extent = map.getExtent()
    const bbox = extent.ymin + "," + extent.xmin + "," + extent.ymax + "," + extent.xmax
    if (bbox !== lastExtent) {
      startWorker(bbox)
      lastExtent = bbox
    }
  } else if (endTimestamp - startTimestamp > timeout) {
    bWorking = false
    timeout += 1000
    worker.terminate() // 终止 web worker
  }
}
 
function startWorker(strBounds) {
  startTimestamp = endTimestamp = new Date().getTime()
  worker = new Worker(window.currentPath + `aqiWorker.js`) // currentPath为当前目录,内置在示例框架中
 
  // 主线程调用worker.postMessage()方法,向 Worker 发消息。
  worker.postMessage({
    bounds: strBounds
  })
  bWorking = true
 
  worker.onmessage = function (event) {
    // Worker返回的结果。
    currentData = event.data.entityTable
    const currTime = event.data.currTime
    if (currentData.length !== 0 && currTime > startTimestamp) {
      createGraphics(currentData)
    }
 
    bWorking = false
    worker.terminate() // 终止 web worker
  }
}
 
function createGraphics(currentData) {
  graphicLayer.clear()
  console.log("加载数据", currentData)
 
  for (let i = currentData.length - 1; i >= 0; i--) {
    const item = currentData[i]
 
    const primitive = new mars3d.graphic.BillboardPrimitive({
      id: item.x,
      name: item.city,
      position: [item.lon, item.lat],
      style: {
        image: mars3d.Util.getCircleImage(item.aqi, {
          color: getColor(item.aqi),
          radius: 25
        }),
        scale: 1,
        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
        verticalOrigin: Cesium.VerticalOrigin.CENTER,
        scaleByDistance: new Cesium.NearFarScalar(10000, 1.0, 900000, 0.3)
      },
      attr: item
    })
    graphicLayer.addGraphic(primitive)
  }
}
 
// 获取色带
function getImageData() {
  const nWidth = 500
  const canvas = document.createElement("canvas")
  canvas.width = nWidth
  canvas.height = nWidth
  const ctx = canvas.getContext("2d")
  ctx.beginPath()
  /* 指定渐变区域 */
  const grad = ctx.createLinearGradient(0, 0, nWidth, 0)
  /* 指定几个颜色 */
  grad.addColorStop(0.05, "rgb(0, 228, 0)") // green
  grad.addColorStop(0.15, "rgb(256, 256, 0)") // yellow
  grad.addColorStop(0.25, "rgb(256, 126, 0)") // orange
  grad.addColorStop(0.35, "rgb(256, 0, 0)") // red
  grad.addColorStop(0.5, "rgb(153, 0, 76)") // purple
  grad.addColorStop(0.8, "rgb(126, 0, 35)") // maroon
  /* 将这个渐变设置为fillStyle */
  ctx.fillStyle = grad
  /* 绘制矩形 */
  ctx.rect(0, 0, nWidth, nWidth)
  ctx.fill()
  return ctx.getImageData(0, 0, nWidth, 1).data
}
 
function getColor(aqi) {
  if (aqi > 500) {
    return "rgba(126,0,35,0.8)"
  } else {
    return `rgba(${imgData[aqi * 4]},${imgData[aqi * 4 + 1]},${imgData[aqi * 4 + 2]},0.8)`
  }
}