mapShow1.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="warp">
  3. <!-- <button @click="getShopMap(8)">加载地图</button> -->
  4. <!-- <button @click="zoomOut()">放大</button><button @click="zoomIn()">缩小</button> -->
  5. <movable-area :scale-area="true">
  6. <movable-view direction="all" :out-of-bounds="true" @change="handleMovableChange" @scale="handleMovableScale" :scale="true" :scale-min="0.5" :scale-max="10" :scale-value="scale">
  7. <!-- <movable-view direction="all" :scale="true" :scale-min="0.5" :scale-max="10" :scale-value="scale"> -->
  8. <!-- <canvas canvas-id="mapCanvas" style="width:100%;height:100%;position:absolute;"></canvas> -->
  9. <canvas canvas-id="mapCanvas" style="width:100%;height:100%;overflow: hidden;"></canvas>
  10. <canvas canvas-id="markerCanvas" style="width:100%;height:100%;position:absolute;z-index:1;"></canvas>
  11. <canvas canvas-id="pathCanvas" style="width:100%;height:100%;position:absolute;z-index:2;"></canvas>
  12. </movable-view>
  13. </movable-area>
  14. </view>
  15. </template>
  16. <script>
  17. import {
  18. DefaultRequest,
  19. IdRequest
  20. } from "@/grpc/base_pb.js"
  21. import mapHelper from '@/utils/mapHelper'
  22. export default {
  23. components: {},
  24. data() {
  25. return {
  26. scale: 1, // 地图图片的缩放比例
  27. translateX: 0, // 地图图片的横向偏移量
  28. translateY: 0, // 地图图片的纵向偏移量,
  29. windowWidth: uni.getSystemInfoSync().windowWidth, // 可视区域宽度
  30. windowHeight: uni.getSystemInfoSync().windowHeight, // 可视区域高度
  31. markerX: 100, // 标记位置 x 坐标
  32. markerY: 100, // 标记位置 y 坐标
  33. path: [
  34. [100, 100],
  35. ], // 运动轨迹点坐标
  36. shopId: 0,
  37. mapUrl: null,
  38. mapInfo: {},
  39. shopList: [],
  40. controlInfoList: []
  41. }
  42. },
  43. computed: {},
  44. mounted() {
  45. },
  46. onLoad() {
  47. // this.getShopList()
  48. this.getShopMap(8)
  49. },
  50. methods: {
  51. zoomOut() {
  52. this.scale++
  53. // this.$nextTick(function() {
  54. // this.scale++
  55. // })
  56. },
  57. zoomIn() {
  58. this.scale--
  59. // this.$nextTick(function() {
  60. // this.scale--
  61. // })
  62. },
  63. async getShopMap(shopId) {
  64. try {
  65. // 创建请求参数并赋值
  66. var request = new IdRequest()
  67. request.setId(shopId)
  68. // 调用客户端相应的grpc方法,发送grpc请求,并接受后台发送回来的返回值
  69. this.$client.assMapInfoDetail(request, {}, async (err, response) => {
  70. if (err) {
  71. console.log(`[assMapInfoDetail] err: code = ${err.code}` +
  72. `, message = "${err.message}"`)
  73. } else {
  74. let res = response.toObject()
  75. console.log('[assMapInfoDetail]', res)
  76. this.mapInfo = res
  77. await mapHelper.handleMapInfo(this, this.mapInfo)
  78. this.handleDrawMap()
  79. }
  80. })
  81. } catch (e) {
  82. console.log('[getShopMap] err', e)
  83. }
  84. },
  85. handleDrawMap() {
  86. this.drawMap();
  87. setInterval(() => {
  88. // 更新标记位置
  89. this.markerX += 10;
  90. this.markerY += 10;
  91. // 更新运动轨迹
  92. this.path.push([this.markerX, this.markerY]);
  93. const markerCtx = uni.createCanvasContext('markerCanvas', this);
  94. markerCtx.clearRect(0, 0, this.windowWidth, this.windowHeight);
  95. this.drawMarker(markerCtx);
  96. markerCtx.draw(false);
  97. const pathCtx = uni.createCanvasContext('pathCanvas', this);
  98. pathCtx.clearRect(0, 0, this.windowWidth, this.windowHeight);
  99. this.drawPath(pathCtx);
  100. pathCtx.draw(false);
  101. }, 1000);
  102. },
  103. handleMovableChange(e) {
  104. console.log("[handleMovableChange] 拖动", e.detail)
  105. // this.translateX = e.detail.x;
  106. // this.translateY = e.detail.y;
  107. // this.drawMap();
  108. },
  109. handleMovableScale(e) {
  110. console.log("[handleMovableScale] 缩放", e.detail)
  111. this.scale = e.detail.scale;
  112. // this.translateX = (1 - e.detail.scale) * (e.detail.fingers[0].x + e.detail.fingers[1].x) / 2;
  113. // this.translateY = (1 - e.detail.scale) * (e.detail.fingers[0].y + e.detail.fingers[1].y) / 2;
  114. // this.drawMap();
  115. },
  116. drawMap() {
  117. console.log("[drawMap]", this.mapUrl, this.windowWidth, this.windowHeight)
  118. const mapCtx = uni.createCanvasContext('mapCanvas', this);
  119. // mapCtx.drawImage('https://ewr1.vultrobjects.com/imgur1/000/001/033/244_7ad_50f.png', 0, 0, this.windowWidth, this.windowHeight-100)
  120. mapCtx.drawImage(this.mapUrl, 0, 0, this.windowWidth, this.windowHeight-100)
  121. // mapCtx.drawImage(this.mapUrl, 0, 0, this.windowWidth * this.scale, this.windowHeight * this.scale, -this.translateX, -this.translateY, this.windowWidth, this.windowHeight);
  122. // mapCtx.drawImage(this.mapUrl, 0, 0, 3113, 3297, -this.translateX, -this.translateY, this.windowWidth, this.windowHeight);
  123. mapCtx.draw();
  124. },
  125. drawMarker(ctx) {
  126. // 绘制标记位置
  127. ctx.beginPath();
  128. ctx.arc(this.markerX, this.markerY, 10/this.scale, 0, 2 * Math.PI);
  129. ctx.setFillStyle('#FF0000');
  130. ctx.fill();
  131. ctx.closePath();
  132. },
  133. drawPath(ctx) {
  134. // 绘制运动轨迹
  135. if (this.path.length <= 1) {
  136. return;
  137. }
  138. ctx.beginPath();
  139. ctx.setStrokeStyle('#0000FF');
  140. ctx.setLineWidth(2/this.scale);
  141. ctx.moveTo(this.path[0][0], this.path[0][1]);
  142. for (let i = 1; i < this.path.length; i++) {
  143. ctx.lineTo(this.path[i][0], this.path[i][1]);
  144. }
  145. ctx.stroke();
  146. ctx.closePath();
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="scss">
  152. .padding {
  153. padding: 0 20rpx;
  154. }
  155. movable-area {
  156. width: 100%;
  157. height: 1000px;
  158. // background-color: #D8D8D8;
  159. overflow: hidden;
  160. }
  161. movable-view {
  162. display: flex;
  163. align-items: center;
  164. justify-content: center;
  165. width: 100%;
  166. height: 100%;
  167. // height: 3000px;
  168. // background-color: #007AFF;
  169. }
  170. .mapView {
  171. // width: 100%;
  172. // height: 300px;
  173. // background-color: seagreen;
  174. }
  175. // .mapMovArea {
  176. // width: 100%;
  177. // height: 300px;
  178. // background-color: cadetblue;
  179. // }
  180. // .mapMovView {
  181. // width: 50%;
  182. // height: 300px;
  183. // background-color: yellow;
  184. // }
  185. .imageMap {
  186. width: 100%;
  187. // width: 600px;
  188. height: 100%;
  189. }
  190. </style>