navto.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view class="container">
  3. <map
  4. id="map"
  5. class="map"
  6. :latitude="querys.latitude"
  7. :longitude="querys.longitude"
  8. :polyline="polyline"
  9. :markers="markers"
  10. scale="15"
  11. show-compass
  12. show-traffic
  13. show-locate
  14. >
  15. </map>
  16. <cover-view class="map-info">
  17. <cover-view class="title">{{ distance }}</cover-view>
  18. <cover-view class="sub-title">{{ taxi_cost }}</cover-view>
  19. </cover-view>
  20. </view>
  21. </template>
  22. <script setup lang="ts">
  23. import { showError } from '@/common/composeabe/ErrorDisplay';
  24. import { useLoadQuerys } from '@/common/composeabe/LoadQuerys';
  25. import AppCofig from '@/common/config/AppCofig';
  26. import { back } from '@/common/utils/PageAction';
  27. import { ref } from 'vue';
  28. import amapFile from '@/libs/amap-wx.130';
  29. const mapCtx = uni.createMapContext('map');
  30. const polyline = ref();
  31. const markers = ref();
  32. const distance = ref('');
  33. const taxi_cost = ref('');
  34. const { querys } = useLoadQuerys({
  35. latitude: 0,
  36. longitude: 0,
  37. }, async (querys) => {
  38. uni.showLoading({ title: '加载中...' });
  39. try {
  40. if (querys.latitude == 0 || querys.longitude == 0) {
  41. throw new Error('传入参数不正确');
  42. }
  43. const curretLocation = await new Promise<{ latitude: number, longitude: number }>((resolve, reject) => {
  44. uni.getLocation({
  45. type: 'gcj02',
  46. success: function(res) {
  47. const latitude = res.latitude;
  48. const longitude = res.longitude;
  49. resolve({ latitude, longitude });
  50. },
  51. fail: (err) => {
  52. let message = err.errMsg
  53. if (err.errMsg.includes('auth deny')) {
  54. message = '请允许定位后,才能为您导航哦';
  55. } else if (err.errMsg.includes('location service off')) {
  56. message = '请未开启定位服务,才能为您导航哦';
  57. }
  58. reject(message);
  59. },
  60. })
  61. })
  62. markers.value = [
  63. {
  64. id: 1,
  65. latitude: querys.latitude,
  66. longitude: querys.longitude,
  67. iconPath: '/static/images/icon_marker.png',
  68. width: 40,
  69. height: 40,
  70. },
  71. {
  72. id: 2,
  73. latitude: curretLocation.latitude,
  74. longitude: curretLocation.longitude,
  75. iconPath: '/static/images/icon_marker2.png',
  76. width: 50,
  77. height: 50,
  78. }
  79. ];
  80. polyline.value = await new Promise((resolve, reject) => {
  81. const myAmapFun = new amapFile.AMapWX({key: AppCofig.amapKey});
  82. myAmapFun.getDrivingRoute({
  83. origin: `${curretLocation.longitude.toFixed(5)},${curretLocation.latitude.toFixed(5)}`,
  84. destination: `${querys.longitude.toFixed(5)},${querys.latitude.toFixed(5)}`,
  85. nosteps: 1,
  86. success: function(data: any){
  87. var points = [];
  88. if(data.paths && data.paths[0] && data.paths[0].steps){
  89. var steps = data.paths[0].steps;
  90. for(var i = 0; i < steps.length; i++){
  91. var poLen = steps[i].polyline.split(';');
  92. for(var j = 0;j < poLen.length; j++){
  93. points.push({
  94. longitude: parseFloat(poLen[j].split(',')[0]),
  95. latitude: parseFloat(poLen[j].split(',')[1])
  96. })
  97. }
  98. }
  99. }
  100. distance.value = '距离您约 ' + formatMeter(data.paths[0].distance) + '米'
  101. taxi_cost.value = '打车约' + parseInt(data.taxi_cost) + '元'
  102. resolve([{
  103. points: points,
  104. color: "#0091ff",
  105. width: 6
  106. }]);
  107. },
  108. fail: function(info: any) {
  109. reject('路线规划失败:' + info);
  110. }
  111. })
  112. });
  113. mapCtx.includePoints({
  114. points: [
  115. { latitude: querys.latitude, longitude: querys.longitude },
  116. { latitude: curretLocation.latitude, longitude: curretLocation.longitude }
  117. ],
  118. padding: [20, 20, 20, 20],
  119. });
  120. } catch (e) {
  121. showError(e, undefined, () => back());
  122. } finally {
  123. uni.hideLoading();
  124. }
  125. });
  126. function formatMeter(n: number) {
  127. if (n > 1000) {
  128. return (n / 1000).toFixed(1) + '千';
  129. }
  130. return n + '';
  131. }
  132. </script>
  133. <style lang="scss">
  134. .container {
  135. position: relative;
  136. display: flex;
  137. flex-direction: column;
  138. height: 100vh;
  139. padding-bottom: 0;
  140. #map {
  141. width: 100%;
  142. height: 100%;
  143. }
  144. .map-info {
  145. position: absolute;
  146. bottom: 50px;
  147. left: 40rpx;
  148. right: 40rpx;
  149. display: flex;
  150. flex-direction: column;
  151. background-color: #fff;
  152. border-radius: 8px;
  153. padding: 20rpx;
  154. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  155. .title {
  156. font-size: 34rpx;
  157. font-weight: bold;
  158. color: #333;
  159. }
  160. .sub-title {
  161. font-size: 26rpx;
  162. margin-top: 10rpx;
  163. color: #666;
  164. }
  165. }
  166. }
  167. </style>