index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <view class="chooseAddress">
  3. <view class="search-box">
  4. <view class="input-box">
  5. <view class="city">
  6. {{ city }}
  7. </view>
  8. <input class="address-input" focus :placeholder="type == 'start' ? '您从哪里上车' : '您想去哪里'" type="text"
  9. v-model="address" />
  10. </view>
  11. </view>
  12. <view class="search-content">
  13. <view class="item" v-for="(item, index) in list" :key="index" @click="chooseAddress(item)">
  14. <view class="info">
  15. <view class="address">
  16. {{ item.title }}
  17. </view>
  18. <view class="desc">
  19. {{ item.address }}
  20. </view>
  21. </view>
  22. <view class="distance">
  23. {{ item._distance | filterDistance }}
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import { debounce } from '@/utils/tool.js'
  31. // 腾讯地图
  32. var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
  33. var qqmapsdk = new QQMapWX({
  34. key: 'KX5BZ-B64RC-RO62W-AMWAZ-VVTC3-YAFXF'
  35. })
  36. export default {
  37. data() {
  38. return {
  39. longitude: 0,
  40. latitude: 0,
  41. city: '定位中',
  42. address: '',
  43. type: '',
  44. list: []
  45. }
  46. },
  47. watch: {
  48. address: debounce(function (keyword) {
  49. let that = this
  50. qqmapsdk.getSuggestion({
  51. keyword,
  52. region_fix: 1,
  53. policy: 1,
  54. location: `${that.latitude},${that.longitude}`,
  55. success(res) {
  56. console.log(res);
  57. that.list = res.data
  58. }
  59. })
  60. })
  61. },
  62. methods: {
  63. setStorage(item){
  64. const history = this.getStorage()
  65. const index = history.findIndex(v => v.title === item.title)
  66. if(index !== -1){
  67. history.splice(index, 1)
  68. }
  69. history.unshift({
  70. _distance: item._distance,
  71. title: item.title,
  72. address: item.address,
  73. location: item.location
  74. })
  75. uni.setStorageSync('addrHistory', JSON.stringify(history))
  76. },
  77. getStorage(){
  78. return JSON.parse(uni.getStorageSync('addrHistory') || '[]')
  79. },
  80. chooseAddress(item) {
  81. try {
  82. console.log(item)
  83. this.setStorage(item)
  84. } catch (error) {
  85. console.log(error)
  86. }
  87. uni.navigateTo({
  88. url: `../index/index?type=${this.type}`,
  89. success: function (res) {
  90. // 通过eventChannel向被打开页面传送数据
  91. res.eventChannel.emit('address', {
  92. latitude: item.location.lat,
  93. longitude: item.location.lng,
  94. address: item.title
  95. })
  96. }
  97. })
  98. }
  99. },
  100. filters: {
  101. filterDistance: function (val) {
  102. return (val / 1000).toFixed(1) + 'km'
  103. }
  104. },
  105. onLoad(val) {
  106. this.type = val.type
  107. },
  108. created() {
  109. this.city = uni.getStorageSync('city')
  110. let that = this
  111. uni.getLocation({
  112. type: 'gcj02',
  113. success(res) {
  114. that.longitude = res.longitude
  115. that.latitude = res.latitude
  116. }
  117. })
  118. this.list = this.getStorage()
  119. }
  120. }
  121. </script>
  122. <style lang="scss" scoped>
  123. .chooseAddress {
  124. .search-box {
  125. position: fixed;
  126. top: 0%;
  127. left: 0%;
  128. width: 100%;
  129. height: 72rpx;
  130. padding: 20rpx 32rpx;
  131. background: #fff;
  132. .input-box {
  133. height: 72rpx;
  134. padding: 16rpx 0;
  135. box-sizing: border-box;
  136. border-radius: 36rpx;
  137. background: #F9F9F9;
  138. display: flex;
  139. font-size: 28rpx;
  140. .city {
  141. flex: 0 0 154rpx;
  142. text-align: center;
  143. color: #222222;
  144. border-right: 2rpx solid #F0F0F0;
  145. ;
  146. }
  147. .address-input {
  148. flex: 1;
  149. color: #AAAAAA;
  150. padding-left: 20rpx;
  151. }
  152. }
  153. }
  154. .search-content {
  155. padding-top: 112rpx;
  156. .item {
  157. display: flex;
  158. align-items: center;
  159. justify-content: space-between;
  160. padding: 36rpx 32rpx;
  161. border-bottom: 1rpx solid #F0F0F0;
  162. .info {
  163. .address {
  164. font-weight: bold;
  165. color: #222222;
  166. font-size: 28rpx;
  167. }
  168. .desc {
  169. font-size: 24rpx;
  170. color: #AAAAAA;
  171. margin-top: 14rpx;
  172. }
  173. }
  174. .distance {
  175. font-size: 28rpx;
  176. color: #AAAAAA;
  177. }
  178. }
  179. }
  180. }
  181. </style>