mapShow2.vue 7.8 KB

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