search.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view>
  3. <view class="plr-36 mt-20">
  4. <u-search v-model="addr" placeholder="请输入地址" @change="bindInput"></u-search>
  5. </view>
  6. <view class="plr-46 ptb-20 bb" @click="search(item)" v-for="(item,i) in list" :key="i" v-if="item&&item.name&&(item.latitude||item.location.length)">
  7. <view class="">
  8. {{item.name}}
  9. </view>
  10. <view class="size-26 gray-2" v-if="item&&item.address">
  11. {{item.id?item.district:''}}{{item.address.length==0?'':item.address}}
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import {mapState} from 'vuex'
  18. import amapFile from '../../libs/amap-wx.js'
  19. export default {
  20. data() {
  21. return {
  22. addr: '',
  23. list: [],
  24. items:[],//历史搜索记录
  25. value:0,//0=没有历史搜索,1=有历史搜索
  26. type:'' //1 起点 2终点 3货运终点
  27. }
  28. },
  29. computed:{
  30. ...mapState(['address'])
  31. },
  32. onLoad(option) {
  33. this.type = option.type
  34. this.location = option.location
  35. this.list = this.address
  36. },
  37. methods: {
  38. bindInput: function(e) {
  39. var that = this;
  40. var keywords = e;
  41. var key = 'bb6f4ed802756f963b37c184ce4e9766';
  42. var myAmapFun = new amapFile.AMapWX({
  43. key: key
  44. });
  45. myAmapFun.getInputtips({
  46. keywords: keywords,
  47. location: that.location,
  48. success: function(data) {
  49. console.log(data);
  50. if (data && data.tips) {
  51. that.list = data.tips
  52. }
  53. }
  54. })
  55. },
  56. search(res) {
  57. console.log(11111111,res);
  58. // {
  59. // "id": "B01730K2X2",
  60. // "name": "郑州站",
  61. // "district": "河南省郑州市二七区",
  62. // "adcode": "410103",
  63. // "location": "113.658097,34.745795",
  64. // "address": "二马路82号",
  65. // "typecode": "150200",
  66. // "city": []
  67. // }
  68. if(res.id){
  69. let city;
  70. // 第一部:判断有没有市
  71. if(res.district.indexOf('市') != -1){
  72. // 第二部:获取第一个市的索引
  73. let num = res.district.indexOf('市')
  74. // 第三部:判断有没有第二个市
  75. if (res.district.indexOf('市', num + 1) == -1) {
  76. if (!res.district.includes('县')) {
  77. if (res.district.split('市')[0].includes('省')) {
  78. //搜索到区
  79. city = res.district.split('市')[0].split('省')[1] + '市'
  80. } else {
  81. city = res.district.split('市')[0] + '市'
  82. }
  83. } else {
  84. //搜索到县
  85. city = res.district.split('市')[1].split('县')[0] + '县'
  86. }
  87. } else {
  88. // 搜索到(县级市)
  89. city = res.district.split('市')[1] + '市'
  90. }
  91. }else{
  92. // 搜索到(直辖县)
  93. city = res.district.split('省')[1]
  94. }
  95. let address = {
  96. address: res.district + res.address,
  97. latitude:res.location.length>0 ? res.location.split(',')[1] : '',
  98. longitude: res.location.length>0 ? res.location.split(',')[0] : '',
  99. name: res.name,
  100. city:city,
  101. district: res.district,
  102. address1: res.address
  103. }
  104. if(this.type==1){
  105. this.$store.commit('getStart', address)
  106. }else if(this.type==2){
  107. this.$store.commit('getEnd', address)
  108. }else if(this.type==3){
  109. this.$store.commit('getHyEnd', address)
  110. }
  111. }else{
  112. if(this.type==1){
  113. this.$store.commit('getStart', res)
  114. }else if(this.type==2){
  115. this.$store.commit('getEnd', res)
  116. }else if(this.type==3){
  117. this.$store.commit('getHyEnd', res)
  118. }
  119. }
  120. uni.navigateBack({
  121. })
  122. }
  123. }
  124. }
  125. </script>
  126. <style lang="scss" scoped>
  127. </style>