HuxingLayer.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * 户型图 图层
  3. * @class HuxingLayer
  4. * @extends {mars3d.layer.GraphicLayer}
  5. */
  6. class HuxingLayer extends mars3d.layer.GraphicLayer {
  7. /**
  8. * 对象添加到地图前创建一些对象的钩子方法,
  9. * 只会调用一次
  10. * @return {void} 无
  11. */
  12. _mountedHook() {}
  13. /**
  14. * 对象添加到地图上的创建钩子方法,
  15. * 每次add时都会调用
  16. * @return {void} 无
  17. */
  18. _addedHook() {
  19. super._addedHook();
  20. this.load();
  21. this.on(mars3d.EventType.click, this._graphic_clickHandler, this);
  22. this._map.on(mars3d.EventType.clickMap, this._map_clickHandler, this);
  23. this.on(mars3d.EventType.mouseOver, this._graphic_mouseOverHandler, this);
  24. this.on(mars3d.EventType.mouseOut, this._graphic_mouseOutHandler, this);
  25. }
  26. /**
  27. * 对象从地图上移除的创建钩子方法,
  28. * 每次remove时都会调用
  29. * @return {void} 无
  30. */
  31. _removedHook() {
  32. super._removedHook();
  33. this.off(mars3d.EventType.click, this._graphic_clickHandler, this);
  34. this._map.off(mars3d.EventType.clickMap, this._map_clickHandler, this);
  35. this.off(mars3d.EventType.mouseOver, this._graphic_mouseOverHandler, this);
  36. this.off(mars3d.EventType.mouseOut, this._graphic_mouseOutHandler, this);
  37. }
  38. //加载数据
  39. load(newOptions = {}) {
  40. this.options = {
  41. ...this.options,
  42. ...newOptions,
  43. };
  44. if (this.options.url) {
  45. Cesium.Resource.fetchJson(this.options)
  46. .then((data) => {
  47. if (this._state == mars3d.State.REMOVED) {
  48. return;
  49. }
  50. this._load_data(data);
  51. })
  52. .otherwise(function (error) {
  53. this.showError("服务出错", error);
  54. });
  55. } else if (this.options.data) {
  56. this._load_data(this.options.data);
  57. } else {
  58. console.warn("HuxinLayer:没有传入 url 或 data 参数,请确认是否有误。");
  59. }
  60. }
  61. _load_data(geojson) {
  62. this.clear();
  63. this._cache_huxin = {};
  64. let arr = mars3d.Util.geoJsonToGraphics(geojson); //解析geojson
  65. for (let i = 0; i < arr.length; i++) {
  66. this.addHuxing(arr[i].positions, arr[i].attr);
  67. }
  68. if (this.options.flyTo) {
  69. this.flyTo({ scale: 2 });
  70. }
  71. this.fire(mars3d.EventType.load);
  72. }
  73. addHuxing(positions, attr) {
  74. if (!positions || positions.length == 0) {
  75. return;
  76. }
  77. var flrH = attr.floorh || 0; //底面高度
  78. var lyrH = attr.layerh || 0; //楼层高度
  79. var primitiveBian = new mars3d.graphic.CorridorPrimitive({
  80. positions: positions,
  81. style: {
  82. height: flrH,
  83. diffHeight: lyrH,
  84. width: 0.2,
  85. cornerType: Cesium.CornerType.MITERED,
  86. color: "rgb(245,255,250)",
  87. },
  88. attr: attr,
  89. });
  90. this.addGraphic(primitiveBian);
  91. var primitiveDi = new mars3d.graphic.PolygonEntity({
  92. positions: positions,
  93. style: {
  94. height: flrH,
  95. diffHeight: 0.1,
  96. color: "rgb(211,211,211)",
  97. outline: true,
  98. outlineWidth: 1,
  99. outlineColor: "#778899",
  100. },
  101. attr: attr,
  102. });
  103. this.addGraphic(primitiveDi);
  104. //记录到缓存中
  105. var loudongHao = attr.LDH; //楼栋号
  106. var cengHao = attr.CH; //层号
  107. this._cache_huxin[loudongHao] = this._cache_huxin[loudongHao] || {};
  108. this._cache_huxin[loudongHao][cengHao] = this._cache_huxin[loudongHao][cengHao] || [];
  109. this._cache_huxin[loudongHao][cengHao].push(primitiveBian);
  110. this._cache_huxin[loudongHao][cengHao].push(primitiveDi);
  111. }
  112. _graphic_clickHandler(event) {
  113. //将上次隐藏的层,恢复下
  114. this._map_clickHandler();
  115. var attr = event.graphic.attr;
  116. var loudongHao = attr.LDH; //楼栋号
  117. var cengHao = attr.CH; //层号
  118. let loudongGraphics = this._cache_huxin[loudongHao];
  119. Object.keys(loudongGraphics).forEach((ceng) => {
  120. let showHu = Number(ceng) <= cengHao; //大于本层的隐藏不显示。
  121. let cengGraphics = loudongGraphics[ceng];
  122. cengGraphics.forEach((huGraphic) => {
  123. huGraphic.show = showHu;
  124. if (!showHu) {
  125. this._lastHideGraphics.push(huGraphic);
  126. }
  127. });
  128. });
  129. }
  130. _map_clickHandler(event) {
  131. //将上次隐藏的层,恢复下
  132. if (this._lastHideGraphics) {
  133. this._lastHideGraphics.forEach((huGraphic) => {
  134. huGraphic.show = true;
  135. });
  136. }
  137. this._lastHideGraphics = [];
  138. }
  139. _graphic_mouseOverHandler(event) {
  140. var graphic = event.graphic;
  141. this.openSmallTooltip(event.windowPosition, graphic.attr.WZ);
  142. }
  143. _graphic_mouseOutHandler(event) {
  144. this.closeSmallTooltip();
  145. }
  146. }