index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <template>
  2. <view class="trip">
  3. <map id="Map" class="map" style="width: 100%;" :longitude="longitude" :latitude="latitude"
  4. :includePoints="includePoints" enable-poi :polyline="polyline" show-location :markers="markers"
  5. @regionchange="mapChange">
  6. <image class="current" v-show="step == 1" src="../../static/current.png" mode=""></image>
  7. <image class="btn" src="../../static/map-position.png" mode="" @click="myPosition"></image>
  8. </map>
  9. <view class="content">
  10. <template v-if="step == 1">
  11. <image class="bg" src="http://hyxhsh.oss-cn-chengdu.aliyuncs.com/63b7c68b71a69169d1b33f92/store/bdb/user/avatar/4wLUSMH6JJgI8c0598e7a5597db70d809806a0f0541f.png/1.png" mode=""></image>
  12. <view class="title" @click="go">
  13. 专业代驾·安全无忧
  14. </view>
  15. <view class="desc">
  16. 首次体验领66代驾券
  17. </view>
  18. <view class="operation">
  19. <view class="start-box" @click="chooseAddress('start')">
  20. <view class="point"></view>
  21. <view class="start">
  22. 您将从 <text class="green">{{form.start}}</text> 出发
  23. </view>
  24. </view>
  25. <view class="end" @click="chooseAddress('end')">
  26. {{form.end||'您想去哪里'}}
  27. </view>
  28. </view>
  29. </template>
  30. <template v-else>
  31. <view class="charging-box">
  32. <view class="box">
  33. <view class="item" @click="chooseAddress('start')">
  34. {{form.start}}
  35. <image class="jiantou" src="../../static/jiantou-icon.png" mode=""></image>
  36. </view>
  37. <view class="item" @click="chooseAddress('end')">
  38. {{form.end}}
  39. <image class="jiantou" src="../../static/jiantou-icon.png" mode=""></image>
  40. </view>
  41. <view class="price" @click="jump('../expense/index')">
  42. 预计 <text class="num">{{form.taxi_fare}}</text>元
  43. <image class="jiantou" src="../../static/jiantou-icon.png" mode=""></image>
  44. </view>
  45. </view>
  46. <view class="btn">
  47. 呼叫秋香代驾
  48. </view>
  49. </view>
  50. </template>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. // 高德地图
  56. var amapFile = require('../../libs/amap-wx.130.js');
  57. var AMap = new amapFile.AMapWX({
  58. key: '0ebb337dd2e77cb84e40525a5b80d640'
  59. });
  60. // 腾讯地图
  61. var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
  62. var qqmapsdk = new QQMapWX({
  63. key: 'KX5BZ-B64RC-RO62W-AMWAZ-VVTC3-YAFXF'
  64. });
  65. import {earthDistance} from '@/utils/tool.js'
  66. export default {
  67. data() {
  68. return {
  69. step: 1, //
  70. map: null,
  71. longitude: 0,
  72. latitude: 0,
  73. polyline: 0,
  74. scale: 19,
  75. flag: false, //
  76. markers: [],
  77. mapCenter: {
  78. longitude: 0,
  79. latitude: 0
  80. },
  81. includePoints: [],
  82. form: {
  83. start: '',
  84. startLocation: {
  85. lat: 0,
  86. lng: 0
  87. },
  88. end: '',
  89. endLocation: {
  90. lat: 0,
  91. lng: 0
  92. },
  93. taxi_fare:0
  94. }
  95. }
  96. },
  97. methods: {
  98. // 跳转
  99. jump(url){
  100. uni.navigateTo({
  101. url
  102. })
  103. },
  104. // 回到我的位置
  105. myPosition() {
  106. let that = this
  107. uni.getLocation({
  108. type: 'gcj02',
  109. success(res) {
  110. that.map.moveToLocation({
  111. longitude: res.longitude,
  112. latitude: res.latitude
  113. })
  114. }
  115. })
  116. },
  117. // 地图拖动
  118. mapChange(val) {
  119. // 绘制出路线后 就不能拖动地图选择起点
  120. if(this.step == 2) return
  121. let that = this
  122. if (val.type == 'end') {
  123. this.mapCenter = val.target.centerLocation
  124. // 高德
  125. // AMap.getPoiAround({
  126. // location:`${val.target.centerLocation.latitude},${val.target.centerLocation.longitude}`,
  127. // success(res){
  128. // console.log(111111111,res)
  129. // }
  130. // })
  131. qqmapsdk.reverseGeocoder({
  132. location: {
  133. longitude: val.target.centerLocation.longitude,
  134. latitude: val.target.centerLocation.latitude
  135. },
  136. poi_options: 'policy=3&address_format=short&radius=30',
  137. get_poi: 1,
  138. success(r) {
  139. console.log('地图拖动',r);
  140. if (r.result.address_reference.landmark_l2._dir_desc == '内') {
  141. }
  142. // 获取最近的店铺
  143. let arr = r.result.pois.sort((a,b)=>{return a._distance - b._distance})
  144. that.form.start = r.result.pois[0].title
  145. that.form.startLocation = r.result.pois[0].location
  146. uni.setStorageSync('city', r.result.address_component.city)
  147. },
  148. fail(e) {
  149. console.log('错误', e);
  150. }
  151. })
  152. }
  153. },
  154. // 选择地址
  155. chooseAddress(type) {
  156. uni.setStorageSync('form', JSON.stringify(this.form))
  157. uni.navigateTo({
  158. url: '../chooseAddress/index?type=' + type
  159. })
  160. },
  161. // 获取当前经纬度
  162. initPosition() {
  163. let that = this
  164. uni.getLocation({
  165. type: 'gcj02',
  166. success(res) {
  167. console.log(res);
  168. that.longitude = res.longitude
  169. that.latitude = res.latitude
  170. that.mapCenter = {
  171. longitude: res.longitude,
  172. latitude: res.latitude
  173. }
  174. that.myPosition()
  175. }
  176. })
  177. },
  178. //绘制路线
  179. go() {
  180. this.step = 2
  181. var _this = this;
  182. //调用距离计算接口
  183. qqmapsdk.direction({
  184. mode: 'driving', //可选值:'driving'(驾车)、'walking'(步行)、'bicycling'(骑行),不填默认:'driving',可不填
  185. //from参数不填默认当前地址
  186. from: `${_this.form.startLocation.lat},${_this.form.startLocation.lng}`,
  187. to: `${_this.form.endLocation.lat},${_this.form.endLocation.lng}`,
  188. success: function(res) {
  189. console.log('地图绘制',res);
  190. _this.form.taxi_fare = res.result.routes[0].taxi_fare.fare
  191. var ret = res;
  192. var coors = ret.result.routes[0].polyline,
  193. pl = [];
  194. //坐标解压(返回的点串坐标,通过前向差分进行压缩)
  195. var kr = 1000000;
  196. for (var i = 2; i < coors.length; i++) {
  197. coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
  198. }
  199. //将解压后的坐标放入点串数组pl中
  200. for (var i = 0; i < coors.length; i += 2) {
  201. pl.push({
  202. latitude: coors[i],
  203. longitude: coors[i + 1]
  204. })
  205. }
  206. //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
  207. _this.latitude = pl[0].latitude
  208. _this.longitude = pl[0].longitude
  209. _this.map.addMarkers({
  210. markers: [{
  211. latitude: _this.form.startLocation.lat,
  212. longitude: _this.form.startLocation.lng,
  213. width: '82rpx',
  214. height: '95rpx',
  215. iconPath: '/static/start.png',
  216. callout:{
  217. content:_this.form.start,
  218. bgColor:'#fff',
  219. color:'#222',
  220. borderRadius:'50%',
  221. fontSize: '28rpx',
  222. padding:'16rpx',
  223. display:'ALWAYS'
  224. }
  225. },
  226. {
  227. latitude: _this.form.endLocation.lat,
  228. longitude: _this.form.endLocation.lng,
  229. width: '82rpx',
  230. height: '95rpx',
  231. iconPath: '/static/end.png',
  232. callout:{
  233. content:_this.form.end,
  234. bgColor:'#fff',
  235. color:'#222',
  236. borderRadius:'50%',
  237. fontSize: '28rpx',
  238. padding:'16rpx',
  239. display:'ALWAYS'
  240. }
  241. }
  242. ]
  243. })
  244. _this.polyline = [{
  245. points: pl,
  246. color: '#39B0FF',
  247. borderColor: '#0071BC',
  248. arrowLine:true,
  249. width: 4
  250. }]
  251. setTimeout(() => {
  252. let includePoints = []
  253. includePoints.push({
  254. latitude: pl[0].latitude+0.01,
  255. longitude: pl[0].longitude+0.01
  256. })
  257. includePoints.push({
  258. latitude: pl[pl.length - 1].latitude - 0.01,
  259. longitude: pl[pl.length - 1].longitude- 0.01
  260. })
  261. _this.includePoints = includePoints
  262. }, 500)
  263. },
  264. fail: function(error) {
  265. console.error(error);
  266. },
  267. complete: function(res) {
  268. console.log(res);
  269. }
  270. });
  271. }
  272. },
  273. onReady() {
  274. this.map = uni.createMapContext('Map', this)
  275. this.initPosition()
  276. },
  277. onLoad(option) {
  278. let that = this
  279. // 获取地址选择页面传来的数据
  280. const eventChannel = this.getOpenerEventChannel();
  281. if(JSON.stringify(eventChannel) !=='{}'){
  282. eventChannel.on('address', function(data) {
  283. if(uni.getStorageSync('form')){
  284. that.form = JSON.parse(uni.getStorageSync('form'))
  285. }
  286. that.form[option.type] = data.address
  287. that.latitude = data.latitude
  288. that.longitude = data.longitude
  289. that.form[option.type + 'Location'].lat = data.latitude
  290. that.form[option.type + 'Location'].lng = data.longitude
  291. // 如果起点和终点都设置,绘制出路线
  292. if(that.form.start&& that.form.end){
  293. that.go()
  294. }
  295. })
  296. }
  297. },
  298. created() {
  299. }
  300. }
  301. </script>
  302. <style lang="scss" scoped>
  303. .trip {
  304. display: flex;
  305. flex-direction: column;
  306. height: 100vh;
  307. .map {
  308. height: 600rpx;
  309. flex: 1;
  310. position: relative;
  311. .current {
  312. position: absolute;
  313. left: 50%;
  314. top: 50%;
  315. transform: translate(-50%, calc(-50% - 40rpx));
  316. width: 40rpx;
  317. height: 68rpx;
  318. }
  319. .btn {
  320. position: absolute;
  321. bottom: 20rpx;
  322. right: 20rpx;
  323. width: 60rpx;
  324. height: 60rpx;
  325. }
  326. }
  327. .content {
  328. width: 100%;
  329. height: 476rpx;
  330. border-radius: 16rpx 16rpx 0 0;
  331. position: relative;
  332. .title {
  333. font-weight: 400;
  334. color: #FFFFFF;
  335. font-size: 40rpx;
  336. position: absolute;
  337. font-weight: bold;
  338. top: 32rpx;
  339. left: 32rpx;
  340. }
  341. .desc {
  342. color: #FFFFFF;
  343. font-size: 28rpx;
  344. position: absolute;
  345. top: 104rpx;
  346. left: 32rpx;
  347. }
  348. .bg {
  349. width: 100%;
  350. height: 476rpx;
  351. border-radius: 16rpx 16rpx 0 0;
  352. }
  353. .operation {
  354. width: 686rpx;
  355. height: 240rpx;
  356. background: #FFFFFF;
  357. border-radius: 20rpx 20rpx 20rpx 20rpx;
  358. position: absolute;
  359. bottom: 40rpx;
  360. left: 50%;
  361. transform: translateX(-50%);
  362. box-sizing: border-box;
  363. display: flex;
  364. flex-direction: column;
  365. align-items: center;
  366. .start-box {
  367. display: flex;
  368. align-items: center;
  369. width: 630rpx;
  370. height: 80rpx;
  371. line-height: 80rpx;
  372. margin-top: 30rpx;
  373. .point {
  374. width: 16rpx;
  375. height: 16rpx;
  376. background: #07AC82;
  377. border-radius: 50%;
  378. }
  379. .start {
  380. font-weight: bold;
  381. color: #222222;
  382. font-size: 28rpx;
  383. margin-left: 20rpx;
  384. display: flex;
  385. .green {
  386. color: #07AC82;
  387. max-width: 400rpx;
  388. white-space: nowrap;
  389. overflow: hidden;
  390. text-overflow: ellipsis;
  391. display: inline-block;
  392. }
  393. }
  394. }
  395. .end {
  396. width: 630rpx;
  397. height: 80rpx;
  398. line-height: 80rpx;
  399. text-indent: 36rpx;
  400. background: #F0F0F0;
  401. border-radius: 16rpx 16rpx 16rpx 16rpx;
  402. font-weight: bold;
  403. color: #222222;
  404. font-size: 36rpx;
  405. margin-top: 20rpx;
  406. }
  407. }
  408. .charging-box {
  409. width: 100%;
  410. height: 100%;
  411. background: linear-gradient(180deg, #FF7365 0%, #F9F9F9 70%,#FFF 100%);
  412. border-radius: 40rpx 40rpx 0rpx 0rpx;
  413. box-sizing: border-box;
  414. padding: 40rpx 32rpx;
  415. .box {
  416. padding: 30rpx;
  417. background: #fff;
  418. border-radius: 20rpx 20rpx 20rpx 20rpx;
  419. .item {
  420. font-weight: bold;
  421. color: #222222;
  422. font-size: 28rpx;
  423. position: relative;
  424. // padding: 14rpx 0;
  425. line-height: 70rpx;
  426. height: 70rpx;
  427. padding-left: 32rpx;
  428. .jiantou{
  429. width: 20rpx;
  430. height: 20rpx;
  431. position: absolute;
  432. top: 50%;
  433. right: 0%;
  434. transform: translateY(-50%);
  435. }
  436. &::before {
  437. content: '';
  438. position: absolute;
  439. top: 50%;
  440. left: 0;
  441. transform: translateY(-50%);
  442. width: 16rpx;
  443. height: 16rpx;
  444. border-radius: 50%;
  445. background: #07AC82;
  446. }
  447. }
  448. .end.item {
  449. border-bottom: 2rpx solid #F0F0F0;
  450. &::before {
  451. background: #FF4A39;
  452. }
  453. }
  454. .price {
  455. font-size: 28rpx;
  456. color: #FF4A39;
  457. margin: 22rpx auto 0;
  458. text-align: center;
  459. position: relative;
  460. .num {
  461. font-weight: bold;
  462. font-size: 30rpx;
  463. }
  464. .jiantou{
  465. width: 20rpx;
  466. height: 20rpx;
  467. position: absolute;
  468. top: 50%;
  469. right: 0%;
  470. transform: translateY(-50%);
  471. }
  472. }
  473. }
  474. .btn {
  475. width: 100%;
  476. line-height: 80rpx;
  477. text-align: center;
  478. background: #FF4A39;
  479. border-radius: 40rpx 40rpx 40rpx 40rpx;
  480. margin-top: 28rpx;
  481. font-weight: bold;
  482. color: #FFFFFF;
  483. font-size: 28rpx;
  484. }
  485. }
  486. }
  487. }
  488. </style>