navto.vue 5.5 KB

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