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
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
let graphicLayer // 矢量图层对象
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 31.794428, lng: 117.235343, alt: 2351.9, heading: 1.6, pitch: -28.8, roll: 0 }
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
  addLayer()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
function addLayer() {
  map.basemap = 2017 // 蓝色底图
 
  // 加载城市模型
  const tilesetLayer = new mars3d.layer.TilesetLayer({
    url: "//data.mars3d.cn/3dtiles/jzw-hefei2/tileset.json",
    maximumScreenSpaceError: 1,
    maximumMemoryUsage: 1024,
    style: {
      color: {
        conditions: [["true", `color("rgba(42, 160, 224, 1)")`]]
      }
    },
    marsJzwStyle: true,
    popup: "all"
  })
  map.addLayer(tilesetLayer)
 
  // 视角切换(分步执行)
  map.setCameraViewList([
    { lat: 31.813938, lng: 117.240085, alt: 3243, heading: 357, pitch: -52 },
    { lat: 31.821728, lng: 117.253605, alt: 1702, heading: 319, pitch: -37 },
    { lat: 31.836674, lng: 117.260762, alt: 1779, heading: 269, pitch: -37 }
  ])
 
  // 创建Graphic图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  // 测试点数据,实际开发时换掉
  const arrPoints = getRandomPoints(1000)
 
  // 热力图 图层
  const heatLayer = new mars3d.layer.HeatLayer({
    positions: arrPoints,
    // 以下为热力图本身的样式参数,可参阅api:https://www.patrick-wied.at/static/heatmapjs/docs.html
    heatStyle: {
      radius: 60,
      blur: 0.85
    },
    // 以下为矩形矢量对象的样式参数
    style: {
      arc: true, // 是否为曲面
      height: 200.0
    }
    // flyTo: true,
  })
  map.addLayer(heatLayer)
 
  // 显示地面对应的点,测试渲染结果的正确性
  for (let i = 0; i < arrPoints.length; i++) {
    const item = arrPoints[i]
 
    const primitive = new mars3d.graphic.PointPrimitive({
      position: [item.lng, item.lat, 90],
      style: {
        color: "#ffff00",
        pixelSize: 7
      }
    })
    graphicLayer.addGraphic(primitive)
  }
  graphicLayer.show = false
}
 
// 显示对应的数据点
export function chkUnderground(val) {
  graphicLayer.show = val
}
 
// 获取bbox矩形区域内的count个随机点
function getRandomPoints(count) {
  const xmin = 117.226189
  const xmax = 117.245831
  const ymin = 31.828858
  const ymax = 31.842967
  const arr = []
  const arrPoint = turf.randomPoint(count, { bbox: [xmin, ymin, xmax, ymax] }).features // 随机点
  for (let i = 0; i < arrPoint.length; i++) {
    const item = arrPoint[i].geometry.coordinates
    const val = Math.floor(Math.random() * 100) // 热力值
    arr.push({ lng: item[0], lat: item[1], value: val })
  }
  return arr
}