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
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
export let graphicLayer // 矢量图层对象
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 31.801072, lng: 117.208356, alt: 1250, heading: 35, pitch: -17 }
  }
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
  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, // 打开建筑物特效(内置Shader代码)
    center: { lat: 31.801072, lng: 117.208356, alt: 1250, heading: 35, pitch: -17 },
    popup: "all"
  })
  map.addLayer(tilesetLayer)
 
  // 创建Graphic图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  const arrData = []
  for (let j = 0; j < 100; ++j) {
    const startPt = randomPoint()
 
    const endPt = startPt.clone()
    endPt.alt = random(1000, 2000)
 
    arrData.push({
      positions: [startPt, endPt],
      style: {
        width: 1,
        material: mars3d.MaterialUtil.createMaterial(mars3d.MaterialType.LineFlowColor, {
          color: "rgb(141,172,172)",
          speed: random(5, 10),
          startTime: random(1000, 3000),
          percent: 0.1,
          alpha: 0.01
        })
      }
    })
  }
 
  // 多个线对象的合并渲染。
  const primitive = new mars3d.graphic.PolylineCombine({
    instances: arrData
  })
  graphicLayer.addGraphic(primitive)
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
 
  graphicLayer.remove()
  graphicLayer = null
}
 
// 取区域内的随机点
function randomPoint() {
  const jd = random(117.208056 * 1000, 117.25548 * 1000) / 1000
  const wd = random(31.816617 * 1000, 31.855756 * 1000) / 1000
  return new mars3d.LngLatPoint(jd, wd, 100)
}
 
// 取随机数字
function random(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min)
}