FloorGraphic.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * 分层的楼栋 模型对象
  3. *
  4. * @class FloorGraphic
  5. * @extends {mars3d.graphic.BasePointEntity}
  6. */
  7. class FloorGraphic extends mars3d.graphic.BasePointEntity {
  8. /**
  9. * 对象添加到图层前创建一些对象的钩子方法,
  10. * 只会调用一次
  11. * @return {void} 无
  12. * @private
  13. */
  14. _mountedHook() {
  15. this._models = [];
  16. let point = this.point; //楼栋位置
  17. let floorHeight = this.style.spacing; //每层层高,单位:米
  18. let floorCount = this.style.count; //总层数(不含楼顶)
  19. //增加楼层
  20. for (let i = 0; i < floorCount; i++) {
  21. let alt = point.alt + i * floorHeight;
  22. let model = new mars3d.graphic.ModelEntity({
  23. position: [point.lng, point.lat, alt],
  24. style: this.style,
  25. attr: {
  26. origAlt: alt, //记录原始高度
  27. },
  28. });
  29. this._models.push(model);
  30. }
  31. //增加楼顶
  32. let topAlt = point.alt + floorCount * floorHeight;
  33. let topModel = new mars3d.graphic.ModelEntity({
  34. position: [point.lng, point.lat, topAlt],
  35. style: {
  36. ...this.style,
  37. url: this.style.topUrl,
  38. },
  39. attr: {
  40. origAlt: topAlt, //记录原始高度
  41. },
  42. });
  43. this._models.push(topModel);
  44. }
  45. /**
  46. * 对象添加到图层上的创建钩子方法,
  47. * 每次add时都会调用
  48. * @return {void} 无
  49. * @private
  50. */
  51. _addedHook() {
  52. this._models.forEach((model) => {
  53. this._layer.addGraphic(model);
  54. });
  55. }
  56. /**
  57. * 对象从图层上移除的创建钩子方法,
  58. * 每次remove时都会调用
  59. * @return {void} 无
  60. * @private
  61. */
  62. _removedHook() {
  63. this._models.forEach((model) => {
  64. this._layer.removeGraphic(model);
  65. });
  66. }
  67. /**
  68. * 展开所有楼层
  69. *
  70. * @param {number} height 展开的每层间隔高度,单位:米
  71. * @param {number} [time=4] 完全展开时间,单位:秒
  72. * @return {void} 无
  73. */
  74. openAll(height, time = 4) {
  75. this.reset();
  76. let point = this.point; //楼栋位置
  77. for (let i = 0; i < this._models.length; i++) {
  78. let model = this._models[i];
  79. let alt = i * height + model.attr.origAlt;
  80. model.moveTo({
  81. position: [point.lng, point.lat, alt],
  82. time: time, //移动的时长(单位 秒)
  83. });
  84. }
  85. }
  86. /**
  87. * 合并所有楼层
  88. *
  89. * @param {number} [time=4] 完成合并时间,单位:秒
  90. * @memberof FloorGraphic
  91. */
  92. mergeAll(time = 4) {
  93. let point = this.point; //楼栋位置
  94. for (let i = 0; i < this._models.length; i++) {
  95. let model = this._models[i];
  96. model.show = true;
  97. model.moveTo({
  98. position: [point.lng, point.lat, model.attr.origAlt],
  99. time: time, //移动的时长(单位 秒)
  100. });
  101. }
  102. }
  103. /**
  104. * 还原重置所有楼层
  105. * @return {void} 无
  106. */
  107. reset() {
  108. let point = this.point; //楼栋位置
  109. for (let i = 0; i < this._models.length; i++) {
  110. let model = this._models[i];
  111. model.position = new mars3d.LatLngPoint(point.lng, point.lat, model.attr.origAlt);
  112. model.show = true;
  113. }
  114. }
  115. /**
  116. * 显示指定楼层
  117. *
  118. * @param {Number} floorNum 指定显示的楼层,第1层开始
  119. * @param {Number} [time=1] 楼层下落需要时间,单位:秒
  120. * @return {void} 无
  121. */
  122. showFloor(floorNum, time = 1) {
  123. floorNum--;
  124. let point = this.point; //楼栋位置
  125. let maxHeight = 120; //顶部落下的高度
  126. for (let i = floorNum; i < this._models.length; i++) {
  127. let model = this._models[i];
  128. model.position = new mars3d.LatLngPoint(point.lng, point.lat, maxHeight);
  129. model.show = false;
  130. }
  131. for (let j = 0; j <= floorNum; j++) {
  132. let model = this._models[j];
  133. model.show = true;
  134. model.moveTo({
  135. position: [point.lng, point.lat, model.attr.origAlt],
  136. time: time, //移动的时长(单位 秒)
  137. });
  138. }
  139. }
  140. }