map.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { MapEngine, type MapEngineStageRect, type MapEngineViewState } from '../../engine/map/mapEngine'
  2. const INTERNAL_BUILD_VERSION = 'map-build-58'
  3. let mapEngine: MapEngine | null = null
  4. function getFallbackStageRect(): MapEngineStageRect {
  5. const systemInfo = wx.getSystemInfoSync()
  6. const width = Math.max(320, systemInfo.windowWidth - 20)
  7. const height = Math.max(280, Math.floor(systemInfo.windowHeight * 0.66))
  8. return {
  9. width,
  10. height,
  11. left: 10,
  12. top: 0,
  13. }
  14. }
  15. Page({
  16. data: {} as MapEngineViewState,
  17. onLoad() {
  18. mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
  19. onData: (patch) => {
  20. this.setData(patch)
  21. },
  22. })
  23. this.setData(mapEngine.getInitialData())
  24. },
  25. onReady() {
  26. this.measureStageAndCanvas()
  27. },
  28. onUnload() {
  29. if (mapEngine) {
  30. mapEngine.destroy()
  31. mapEngine = null
  32. }
  33. },
  34. measureStageAndCanvas() {
  35. const page = this
  36. const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
  37. const fallbackRect = getFallbackStageRect()
  38. const rect: MapEngineStageRect = {
  39. width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
  40. height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
  41. left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
  42. top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
  43. }
  44. const currentEngine = mapEngine
  45. if (!currentEngine) {
  46. return
  47. }
  48. currentEngine.setStage(rect)
  49. const canvasQuery = wx.createSelectorQuery().in(page)
  50. canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
  51. canvasQuery.exec((canvasRes) => {
  52. const canvasRef = canvasRes[0] as any
  53. if (!canvasRef || !canvasRef.node) {
  54. page.setData({
  55. statusText: `WebGL 引擎初始化失败 (${INTERNAL_BUILD_VERSION})`,
  56. })
  57. return
  58. }
  59. const dpr = wx.getSystemInfoSync().pixelRatio || 1
  60. try {
  61. currentEngine.attachCanvas(canvasRef.node, rect.width, rect.height, dpr)
  62. } catch (error) {
  63. page.setData({
  64. statusText: `WebGL 初始化失败 (${INTERNAL_BUILD_VERSION})`,
  65. })
  66. }
  67. })
  68. }
  69. const query = wx.createSelectorQuery().in(page)
  70. query.select('.map-stage').boundingClientRect()
  71. query.exec((res) => {
  72. const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
  73. applyStage(rect)
  74. })
  75. },
  76. handleTouchStart(event: WechatMiniprogram.TouchEvent) {
  77. if (mapEngine) {
  78. mapEngine.handleTouchStart(event)
  79. }
  80. },
  81. handleTouchMove(event: WechatMiniprogram.TouchEvent) {
  82. if (mapEngine) {
  83. mapEngine.handleTouchMove(event)
  84. }
  85. },
  86. handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
  87. if (mapEngine) {
  88. mapEngine.handleTouchEnd(event)
  89. }
  90. },
  91. handleTouchCancel() {
  92. if (mapEngine) {
  93. mapEngine.handleTouchCancel()
  94. }
  95. },
  96. handleRecenter() {
  97. if (mapEngine) {
  98. mapEngine.handleRecenter()
  99. }
  100. },
  101. handleRotateStep() {
  102. if (mapEngine) {
  103. mapEngine.handleRotateStep()
  104. }
  105. },
  106. handleRotationReset() {
  107. if (mapEngine) {
  108. mapEngine.handleRotationReset()
  109. }
  110. },
  111. handleSetManualMode() {
  112. if (mapEngine) {
  113. mapEngine.handleSetManualMode()
  114. }
  115. },
  116. handleSetNorthUpMode() {
  117. if (mapEngine) {
  118. mapEngine.handleSetNorthUpMode()
  119. }
  120. },
  121. handleSetHeadingUpMode() {
  122. if (mapEngine) {
  123. mapEngine.handleSetHeadingUpMode()
  124. }
  125. },
  126. handleAutoRotateCalibrate() {
  127. if (mapEngine) {
  128. mapEngine.handleAutoRotateCalibrate()
  129. }
  130. },
  131. })