index.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {
  2. Cartesian3,
  3. HeadingPitchRoll,
  4. HeightReference,
  5. Matrix4,
  6. Model,
  7. ScreenSpaceEventHandler,
  8. ScreenSpaceEventType,
  9. Transforms,
  10. VerticalOrigin,
  11. } from "cesium";
  12. import type { PlotFuncI, PointArr, BasePointI } from "../../interface";
  13. import { getCatesian3FromPX, cartesianToLonlat } from "../../tools";
  14. import emitter from "@/mitt";
  15. class BasePoint implements BasePointI {
  16. type: string;
  17. baseType: string;
  18. objId: number;
  19. handler: any;
  20. state: number; //state用于区分当前的状态 0 为删除 1为添加 2为编辑
  21. floatPoint: any;
  22. pointPrimitive: any;
  23. modifyHandler: any;
  24. pointList: any;
  25. constructor(obj: BasePointI) {
  26. this.type = obj.type;
  27. this.baseType = "point";
  28. this.objId = obj.objId;
  29. this.handler = obj.handler;
  30. this.pointPrimitive = obj.pointPrimitive;
  31. this.state = obj.state; //state用于区分当前的状态 0 为删除 1为添加 2为编辑
  32. this.floatPoint = obj.floatPoint;
  33. this.modifyHandler = obj.modifyHandler;
  34. this.pointList = obj.pointList;
  35. }
  36. }
  37. // * marker广告牌
  38. class Marker extends BasePoint implements PlotFuncI {
  39. constructor() {
  40. super({
  41. type: "Marker",
  42. objId: Number(
  43. new Date().getTime() + "" + Number(Math.random() * 1000).toFixed(0)
  44. ),
  45. handler: new ScreenSpaceEventHandler(window.Viewer.scene.canvas),
  46. state: -1,
  47. pointPrimitive: null,
  48. floatPoint: null,
  49. modifyHandler: null,
  50. pointList: [],
  51. });
  52. }
  53. disable() {
  54. if (this.pointPrimitive) {
  55. window.Viewer.billboards.remove(this.pointPrimitive);
  56. window.Viewer.billboards.remove(this.floatPoint);
  57. this.pointPrimitive = null;
  58. }
  59. this.state = -1;
  60. this.stopDraw();
  61. }
  62. stopDraw() {
  63. if (this.handler) {
  64. this.handler.removeInputAction(ScreenSpaceEventType.LEFT_CLICK);
  65. this.handler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE);
  66. this.handler.destroy();
  67. this.handler = null;
  68. }
  69. if (this.modifyHandler) {
  70. this.modifyHandler.removeInputAction(ScreenSpaceEventType.LEFT_CLICK);
  71. this.modifyHandler.removeInputAction(ScreenSpaceEventType.MOUSE_MOVE);
  72. this.modifyHandler.destroy();
  73. this.modifyHandler = null;
  74. }
  75. }
  76. startDraw() {
  77. this.state = 1;
  78. this.handler.setInputAction((evt: any) => {
  79. const cartesian = getCatesian3FromPX(evt.position);
  80. if (!cartesian) return;
  81. this.state = -1;
  82. this.pointList.push(cartesian);
  83. this.pointPrimitive = this.showPrimitiveOnMap(cartesian);
  84. this.floatPoint.show = false;
  85. this.stopDraw();
  86. emitter.emit("drawEnd");
  87. }, ScreenSpaceEventType.LEFT_CLICK);
  88. this.handler.setInputAction((evt: any) => {
  89. const cartesian = getCatesian3FromPX(evt.endPosition);
  90. if (!cartesian) return;
  91. if (this.floatPoint) this.floatPoint.position = cartesian;
  92. else this.floatPoint = this.creatPoint(cartesian);
  93. }, ScreenSpaceEventType.MOUSE_MOVE);
  94. }
  95. startModify() {
  96. if (!this.modifyHandler)
  97. this.modifyHandler = new ScreenSpaceEventHandler(
  98. window.Viewer.scene.canvas
  99. );
  100. this.state = 2;
  101. this.floatPoint.show = true;
  102. this.modifyHandler.setInputAction((evt: any) => {
  103. this.floatPoint.show = false;
  104. this.state = -1;
  105. this.pointPrimitive.position = getCatesian3FromPX(evt.position);
  106. this.stopDraw();
  107. emitter.emit("modifiedEnd");
  108. }, ScreenSpaceEventType.LEFT_CLICK);
  109. this.modifyHandler.setInputAction((evt: any) => {
  110. const cartesian = getCatesian3FromPX(evt.endPosition);
  111. if (!cartesian) return;
  112. if (this.floatPoint) {
  113. this.floatPoint.position = cartesian;
  114. this.pointList = cartesian;
  115. } else {
  116. return;
  117. }
  118. }, ScreenSpaceEventType.MOUSE_MOVE);
  119. }
  120. createByData(data: PointArr) {
  121. this.pointList = [];
  122. this.state = -1;
  123. this.floatPoint = null;
  124. this.modifyHandler = null;
  125. this.pointList = Cartesian3.fromDegreesArray(data);
  126. this.pointPrimitive = this.showPrimitiveOnMap(this.pointList);
  127. this.pointPrimitive.objId = this.objId;
  128. }
  129. getLnglats() {
  130. return cartesianToLonlat(this.pointList[0]);
  131. }
  132. getPositions() {
  133. return this.pointList;
  134. }
  135. creatPoint(cartesian: PointArr) {
  136. return window.Viewer.billboards.add({
  137. position: cartesian,
  138. image: "/src/assets/icon/point.png",
  139. verticalOrigin: VerticalOrigin.BOTTOM,
  140. heightReference: HeightReference.CLAMP_TO_GROUND,
  141. });
  142. }
  143. showPrimitiveOnMap(positons: Cartesian3) {
  144. return window.Viewer.billboards.add({
  145. position: positons,
  146. id: this.objId,
  147. image: "/src/assets/icon/mark.png",
  148. verticalOrigin: VerticalOrigin.BOTTOM,
  149. heightReference: HeightReference.CLAMP_TO_GROUND,
  150. });
  151. }
  152. showPrimitiveModelOnMap(url: string, modelMatrix?: Matrix4 | undefined) {
  153. return window.Viewer.scene.primitives.add(
  154. Model.fromGltf({
  155. id: this.objId,
  156. url,
  157. modelMatrix:
  158. modelMatrix ??
  159. Transforms.headingPitchRollToFixedFrame(
  160. this.pointList as Cartesian3,
  161. new HeadingPitchRoll(0, 0, 0)
  162. ),
  163. heightReference: HeightReference.CLAMP_TO_GROUND,
  164. scene: window.Viewer.scene,
  165. })
  166. );
  167. }
  168. }
  169. export { Marker };