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
import * as mars3d from "mars3d"
 
let map3d
let map2d
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map3d = mapInstance // 记录map
  map3d.camera.percentageChanged = 0.01
 
  globalNotify("已知问题提示", `三维事件目前监听不灵敏,视角同步不够平滑。 `)
 
  creatMap2D()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  unbind3dEvent()
  unbind2dEvent()
 
  map3d = null
 
  map2d.destroy()
  map2d = null
}
 
function creatMap2D() {
  showLoading()
 
  const mapDiv = mars3d.DomUtil.create("div", "", document.body)
  mapDiv.setAttribute("id", "centerDiv2D")
 
  const map2dDiv = mars3d.DomUtil.create("div", "", mapDiv)
  map2dDiv.setAttribute("id", "map2d")
  map2dDiv.setAttribute("class", "mars2d-container")
 
  const configUrl = "http://mars2d.cn/config/config.json"
  mars2d.Util.fetchJson({ url: configUrl })
    .then(function (data) {
      // 构建地图
      map2d = new mars2d.Map("map2d", data.mars2d)
      bind2dEvent(map2d)
      bind3dEvent()
 
      _map2d_extentChangeHandler()
 
      viewTo23D() // 默认
 
      hideLoading()
    })
    .catch(function (error) {
      hideLoading()
 
      console.log("构建地图错误", error)
      globalMsg(error && error.message, "error")
    })
}
 
// 二维地图变化事件
function bind2dEvent() {
  map2d.on("drag", _map2d_extentChangeHandler, this)
  map2d.on("zoomend", _map2d_extentChangeHandler, this)
}
 
function unbind2dEvent() {
  map2d.off("drag", _map2d_extentChangeHandler, this)
  map2d.off("zoomend", _map2d_extentChangeHandler, this)
}
 
function _map2d_extentChangeHandler(e) {
  const bounds = map2d.getBounds()
  const extent = {
    xmin: bounds.getWest(),
    xmax: bounds.getEast(),
    ymin: bounds.getSouth(),
    ymax: bounds.getNorth()
  }
  console.log(`'二维地图变化了,区域: ${JSON.stringify(extent)} `)
 
  unbind3dEvent()
  map3d.camera.setView({
    destination: Cesium.Rectangle.fromDegrees(extent.xmin, extent.ymin, extent.xmax, extent.ymax)
  })
 
  bind3dEvent()
}
 
// 三维地图相机移动结束事件
function bind3dEvent() {
  map3d.on(mars3d.EventType.cameraChanged, camera_moveEndHandler, this)
}
 
function unbind3dEvent() {
  map3d.off(mars3d.EventType.cameraChanged, camera_moveEndHandler, this)
}
 
function camera_moveEndHandler(e) {
  const point = map3d.getCenter() // 范围对象
  const level = map3d.level
  console.log(`'三维地图变化了,位置: ${point.toString()},层级 ${level} `)
 
  unbind2dEvent()
 
  map2d.setView([point.lat, point.lng], level, { animate: false })
 
  bind2dEvent()
}
 
export function viewTo3d() {
  const to3dDom = document.getElementById("centerDiv3D")
  const to2dDom = document.getElementById("centerDiv2D")
  to2dDom.style.display = "none"
  to3dDom.style.display = "block"
  to3dDom.style.left = "0"
  to3dDom.style.width = "100%"
}
 
export function viewTo2d() {
  const to3dDom = document.getElementById("centerDiv3D")
  const to2dDom = document.getElementById("centerDiv2D")
  to3dDom.style.display = "none"
  to2dDom.style.display = "block"
  to2dDom.style.width = "100%"
 
  if (map2d) {
    map2d.invalidateSize(false)
  }
}
 
export function viewTo23D() {
  const to3dDom = document.getElementById("centerDiv3D")
  const to2dDom = document.getElementById("centerDiv2D")
  to3dDom.style.width = "50%"
  to2dDom.style.width = "50%"
  to3dDom.style.left = "50%"
  to3dDom.style.display = "block"
  to2dDom.style.display = "block"
 
  if (map2d) {
    map2d.invalidateSize(false)
  }
}