index.vue 12 KB

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