localSearch.vue 3.1 KB

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