localSearch.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view class="localSearchBox">
  3. <view class="header">
  4. <!-- <text @click="back()">取消</text> -->
  5. <!-- <text>选择位置</text> -->
  6. <text></text>
  7. </view>
  8. <view class="searchBox">
  9. <uni-easyinput prefixIcon="search" @confirm="searchAddress" placeholder="请输入地址"></uni-easyinput>
  10. </view>
  11. <view class="listBox">
  12. <view class="listItem" v-for="(item, index) in list" :key="item.uid" @click="addressClick(index)">
  13. <view class="shortAddress">
  14. <view class="shortTitle">{{ item.name }}</view>
  15. <image src="../../static/image/checked.png" v-if="defaultIndex === index" class="checkedImg">
  16. </image>
  17. </view>
  18. <view class="realAddress">{{ item.address }}</view>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import bmap from '../../utils/bmap-wx.js'
  25. import {
  26. config
  27. } from '../../utils/config.js'
  28. export default {
  29. name: 'localSearch',
  30. data() {
  31. return {
  32. defaultIndex: -1,
  33. list: [],
  34. BMap: null,
  35. latlngStr: ''
  36. };
  37. },
  38. methods: {
  39. initIndex() {
  40. this.defaultIndex = 0
  41. },
  42. // back() {
  43. // this.$emit('closeSelectAddress', {});
  44. // },
  45. submitAddress() {
  46. let address = this.list[this.defaultIndex].address
  47. this.$emit('changeAddress', address);
  48. },
  49. addressClick(index) {
  50. this.defaultIndex = index;
  51. this.submitAddress()
  52. },
  53. getLocation() {
  54. wx.getFuzzyLocation({
  55. type: 'gcj02',
  56. success: res => {
  57. console.log(res, 'res')
  58. this.latlngStr = `${res.latitude},${res.longitude}`
  59. this.getLocationInfo()
  60. }
  61. })
  62. },
  63. searchAddress(e) {
  64. this.getLocationInfo(e)
  65. },
  66. getLocationInfo(addressKey = '') {
  67. if (addressKey == "") {
  68. this.BMap.search({
  69. "query": addressKey,
  70. location: this.latlngStr,
  71. page_size: 20,
  72. page_index: 1,
  73. success: res => {
  74. console.log(res);
  75. this.list = res.originalData.results
  76. },
  77. fail: err => {
  78. console.log(err);
  79. }
  80. })
  81. } else {
  82. this.BMap.suggestion({
  83. "query": addressKey,
  84. success: res => {
  85. console.log(res);
  86. this.list = res.result || []
  87. },
  88. fail: err => {
  89. console.log(err);
  90. }
  91. })
  92. }
  93. }
  94. },
  95. created() {
  96. this.BMap = new bmap.BMapWX({
  97. ak: config.BMapKey
  98. });
  99. this.getLocation()
  100. }
  101. };
  102. </script>
  103. <style lang="scss">
  104. .localSearchBox {
  105. box-sizing: border-box;
  106. padding: 0 32rpx;
  107. width: 100%;
  108. height: 100%;
  109. .header {
  110. display: flex;
  111. justify-content: space-between;
  112. align-items: center;
  113. height: 50px;
  114. }
  115. .searchBox {
  116. height: 36px;
  117. }
  118. .listBox {
  119. width: 100%;
  120. height: calc(100% - 80px);
  121. overflow-x: hidden;
  122. overflow-y: scroll;
  123. box-sizing: border-box;
  124. padding-bottom: 10px;
  125. .listItem {
  126. margin-top: 25px;
  127. .shortAddress {
  128. color: #151b26;
  129. display: flex;
  130. font-size: 16px;
  131. font-weight: 400;
  132. line-height: 24px;
  133. .shortTitle {
  134. flex: 1;
  135. overflow: hidden;
  136. text-overflow: ellipsis;
  137. white-space: nowrap;
  138. }
  139. .checkedImg {
  140. height: 24px;
  141. width: 24px;
  142. }
  143. }
  144. .realAddress {
  145. color: #5c5f66;
  146. font-size: 14px;
  147. font-weight: 400;
  148. line-height: 20px;
  149. margin-top: 12px;
  150. }
  151. }
  152. }
  153. }
  154. </style>