move.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view>
  3. <map
  4. id="map"
  5. scale="15"
  6. :longitude="longitude"
  7. :latitude="latitude"
  8. :enable-poi="true"
  9. class="map"
  10. :style="mapStyle"
  11. :polyline="polyline"
  12. :markers="markers"
  13. @longpress="showHandle"
  14. >
  15. <!-- <cover-image class="location" src="../static/workbench/location.png" @tap="returnLocationHandle()" /> -->
  16. <cover-view class="information-container" v-show="infoStatus">
  17. <cover-image src="../static/move/close-icon.png" class="close-icon" @tap="hideHandle"></cover-image>
  18. <cover-view class="info">
  19. <cover-view class="label">剩余</cover-view>
  20. <cover-view class="value">{{ distance }}公里</cover-view>
  21. <cover-view class="label">,预计</cover-view>
  22. <cover-view class="value">{{ duration }}分钟</cover-view>
  23. </cover-view>
  24. </cover-view>
  25. </map>
  26. </view>
  27. </template>
  28. <script>
  29. let QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
  30. let qqmapsdk;
  31. export default {
  32. data() {
  33. return {
  34. orderId: null,
  35. status: null,
  36. mode: null,
  37. map: null,
  38. mapStyle: '',
  39. startLatitude: 0,
  40. startLongitude: 0,
  41. endLatitude: 0,
  42. endLongitude: 0,
  43. latitude: 0,
  44. longitude: 0,
  45. targetLatitude: 0,
  46. targetLongitude: 0,
  47. distance: 0,
  48. duration: 0,
  49. polyline: [],
  50. markers: [],
  51. timer: null,
  52. infoStatus: true
  53. };
  54. },
  55. methods: {
  56. formatPolyline(polyline) {
  57. let coors = polyline;
  58. let pl = [];
  59. //坐标解压(返回的点串坐标,通过前向差分进行压缩)
  60. const kr = 1000000;
  61. for (let i = 2; i < coors.length; i++) {
  62. coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
  63. }
  64. //将解压后的坐标放入点串数组pl中
  65. for (let i = 0; i < coors.length; i += 2) {
  66. pl.push({
  67. longitude: coors[i + 1],
  68. latitude: coors[i]
  69. });
  70. }
  71. return pl;
  72. },
  73. calculateLine: function(ref) {
  74. if (ref.latitude == 0 || ref.longitude == 0) {
  75. return;
  76. }
  77. qqmapsdk.direction({
  78. mode: ref.mode,
  79. from: {
  80. latitude: ref.latitude,
  81. longitude: ref.longitude
  82. },
  83. to: {
  84. latitude: ref.targetLatitude,
  85. longitude: ref.targetLongitude
  86. },
  87. success: function(resp) {
  88. let route = resp.result.routes[0];
  89. let distance = route.distance;
  90. let duration = route.duration;
  91. let polyline = route.polyline;
  92. ref.distance = Math.ceil((distance / 1000) * 10) / 10;
  93. ref.duration = duration;
  94. let points = ref.formatPolyline(polyline);
  95. ref.polyline = [
  96. {
  97. points: points,
  98. width: 6,
  99. color: '#05B473',
  100. arrowLine: true
  101. }
  102. ];
  103. ref.markers = [
  104. {
  105. id: 1,
  106. latitude: ref.latitude,
  107. longitude: ref.longitude,
  108. width: 35,
  109. height: 35,
  110. anchor: {
  111. x: 0.5,
  112. y: 0.5
  113. },
  114. iconPath: '../static/move/driver-icon.png'
  115. }
  116. ];
  117. },
  118. fail: function(error) {
  119. console.log(error);
  120. }
  121. });
  122. },
  123. hideHandle: function() {
  124. this.infoStatus = false;
  125. },
  126. showHandle: function() {
  127. this.infoStatus = true;
  128. }
  129. },
  130. onLoad: function(options) {
  131. let that = this;
  132. that.orderId = options.orderId;
  133. qqmapsdk = new QQMapWX({
  134. key: that.tencent.map.key
  135. });
  136. let windowHeight = uni.getSystemInfoSync().windowHeight;
  137. that.mapStyle = `height:${windowHeight}px`;
  138. },
  139. onShow: function() {
  140. let that = this;
  141. uni.$on('updateLocation', function(location) {
  142. if (location != null) {
  143. that.latitude = location.latitude;
  144. that.longitude = location.longitude;
  145. }
  146. });
  147. let data = {
  148. orderId: that.orderId
  149. };
  150. that.ajax(that.url.searchOrderForMoveById, 'POST', data, function(resp) {
  151. let result = resp.data.result;
  152. let startPlaceLocation = JSON.parse(result.startPlaceLocation);
  153. that.startLatitude = startPlaceLocation.latitude;
  154. that.startLongitude = startPlaceLocation.longitude;
  155. let endPlaceLocation = JSON.parse(result.endPlaceLocation);
  156. that.endLatitude = endPlaceLocation.latitude;
  157. that.endLongitude = endPlaceLocation.longitude;
  158. let status = result.status;
  159. if (status == 2) {
  160. that.targetLatitude = that.startLatitude;
  161. that.targetLongitude = that.startLongitude;
  162. that.mode = 'bicycling';
  163. } else if (status == 3 || status == 4) {
  164. that.targetLatitude = that.endLatitude;
  165. that.targetLongitude = that.endLongitude;
  166. that.mode = 'driving';
  167. }
  168. that.calculateLine(that);
  169. that.timer = setInterval(function() {
  170. that.calculateLine(that);
  171. }, 6000);
  172. });
  173. },
  174. onHide: function() {
  175. let that = this;
  176. uni.$off('updateLocation');
  177. clearInterval(that.timer);
  178. that.timer = null;
  179. }
  180. };
  181. </script>
  182. <style lang="less">
  183. @import url('move.less');
  184. </style>