master
2022-03-21 1292f810644e422357734d1616761eb920903f45
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
/**
 * 分层的楼栋 模型对象
 *
 * @class FloorGraphic
 * @extends {mars3d.graphic.BasePointEntity}
 */
class FloorGraphic extends mars3d.graphic.BasePointEntity {
  /**
   * 对象添加到图层前创建一些对象的钩子方法,
   * 只会调用一次
   * @return {void}  无
   * @private
   */
  _mountedHook() {
    this._models = []
 
    const point = this.point // 楼栋位置
    const floorHeight = this.style.spacing // 每层层高,单位:米
    const floorCount = this.style.count // 总层数(不含楼顶)
 
    // 增加楼层
    for (let i = 0; i < floorCount; i++) {
      const alt = point.alt + i * floorHeight
      const model = new mars3d.graphic.ModelEntity  ({
        position: [point.lng, point.lat, alt],
        style: this.style,
        attr: {
          origAlt: alt // 记录原始高度
        }
      })
      this._models.push(model)
    }
 
    // 增加楼顶
    const topAlt = point.alt + floorCount * floorHeight
    const topModel = new mars3d.graphic.ModelEntity({
      position: [point.lng, point.lat, topAlt],
      style: {
        ...this.style,
        url: this.style.topUrl
      },
      attr: {
        origAlt: topAlt // 记录原始高度
      }
    })
    this._models.push(topModel)
  }
 
  /**
   * 对象添加到图层上的创建钩子方法,
   * 每次add时都会调用
   * @return {void}  无
   * @private
   */
  _addedHook() {
    this._models.forEach((model) => {
      this._layer.addGraphic(model)
    })
  }
 
  /**
   * 对象从图层上移除的创建钩子方法,
   * 每次remove时都会调用
   * @return {void}  无
   * @private
   */
  _removedHook() {
    this._models.forEach((model) => {
      this._layer.removeGraphic(model)
    })
  }
 
  /**
   * 展开所有楼层
   *
   * @param {number} height 展开的每层间隔高度,单位:米
   * @param {number} [time=4] 完全展开时间,单位:秒
   * @return {void}  无
   */
  openAll(height, time = 4) {
    this.reset()
 
    const point = this.point // 楼栋位置
 
    for (let i = 0; i < this._models.length; i++) {
      const model = this._models[i]
 
      const alt = i * height + model.attr.origAlt
      model.moveTo({
        position: [point.lng, point.lat, alt],
        time: time // 移动的时长(单位 秒)
      })
    }
  }
 
  /**
   * 合并所有楼层
   *
   * @param {number} [time=4] 完成合并时间,单位:秒
   * @memberof FloorGraphic
   * @returns {void}
   */
  mergeAll(time = 4) {
    const point = this.point // 楼栋位置
 
    for (let i = 0; i < this._models.length; i++) {
      const model = this._models[i]
 
      model.show = true
      model.moveTo({
        position: [point.lng, point.lat, model.attr.origAlt],
        time: time // 移动的时长(单位 秒)
      })
    }
  }
 
  /**
   * 还原重置所有楼层
   * @return {void}  无
   */
  reset() {
    const point = this.point // 楼栋位置
 
    for (let i = 0; i < this._models.length; i++) {
      const model = this._models[i]
 
      model.position = new mars3d.LatLngPoint(point.lng, point.lat, model.attr.origAlt)
      model.show = true
    }
  }
 
  /**
   * 显示指定楼层
   *
   * @param {Number} floorNum 指定显示的楼层,第1层开始
   * @param {Number} [time=1] 楼层下落需要时间,单位:秒
   * @return {void}  无
   */
  showFloor(floorNum, time = 1) {
    floorNum--
 
    const point = this.point // 楼栋位置
    const maxHeight = 120 // 顶部落下的高度
 
    for (let i = floorNum; i < this._models.length; i++) {
      const model = this._models[i]
      model.position = new mars3d.LatLngPoint(point.lng, point.lat, maxHeight)
      model.show = false
    }
 
    for (let j = 0; j <= floorNum; j++) {
      const model = this._models[j]
 
      model.show = true
      model.moveTo({
        position: [point.lng, point.lat, model.attr.origAlt],
        time: time // 移动的时长(单位 秒)
      })
    }
  }
}