addressSearch.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="page">
  3. <view class="search">
  4. <view class="region dis a-c j-s" style="position: absolute; z-index: 9;">
  5. <text>{{city}}</text>
  6. <view style="width: 1px;height: 60%;background-color: #999;"></view>
  7. </view>
  8. <u-search style="position: relative;" placeholder="请输入位置/地名等" v-model="address" :height='80'
  9. :animation='true' :input-style="{paddingLeft: '50px'}" @search="search" @custom="search">
  10. </u-search>
  11. </view>
  12. <view class="addressContent dis f-c">
  13. <view class="dis f-c" style="margin-bottom: 15px;" v-for="(item,index) in addressList" :key="index"
  14. @click="addressClick(item)">
  15. <text style="font-size: 16px;">{{item.name}}</text>
  16. <view class="dis a-c" style="color: #999;">
  17. <text>{{item.distance}}</text>
  18. <view style="width: 1px;height: 16px;background-color: #999;margin: 0 10px;"></view>
  19. <text>{{item.address}}</text>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. city: "",
  30. address: "",
  31. mapKey: '197d1a12301a306f82533a23651807b8',
  32. latitude: 0,
  33. longitude: 0,
  34. addressList: [],
  35. }
  36. },
  37. async onLoad(params) {
  38. if (!!params) {
  39. let data = JSON.parse(params.info);
  40. this.latitude = data.latitude;
  41. this.longitude = data.longitude;
  42. this.city = data.address.city;
  43. }
  44. },
  45. methods: {
  46. search(val) {
  47. this.address = val;
  48. this.addressConversion();
  49. },
  50. addressConversion() {
  51. uni.request({
  52. url: `https://restapi.amap.com/v3/place/around?key=${this.mapKey}&location=${this.longitude},${this.latitude}&radius=10000&types=&city=${this.city}&keywords=${this.address}`,
  53. method: "GET",
  54. success: (res) => {
  55. this.addressList = res.data.pois;
  56. this.addressList.map(val => {
  57. val.distance = this.conversion(val.distance);
  58. return val;
  59. })
  60. }
  61. });
  62. },
  63. conversion(data) {
  64. if (data < 1000) {
  65. return data + 'm'
  66. } else {
  67. return data / 1000 + 'km'
  68. }
  69. },
  70. addressClick(val) {
  71. let data = val.location.split(',')
  72. //选中之后返回上一页并修改数据
  73. let pages = getCurrentPages(); //获取所有页面栈实例列表
  74. let nowPage = pages[pages.length - 1]; //当前页页面实例
  75. let prevPage = pages[pages.length - 2]; //上一页页面实例
  76. prevPage.$vm.city = val.cityname; //城市
  77. prevPage.$vm.keywords = val.name; //关键词
  78. prevPage.$vm.latitude = data[1]; //经度
  79. prevPage.$vm.longitude = data[0]; //纬度
  80. uni.navigateBack({
  81. delta: 1
  82. });
  83. },
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. .mymap__map {
  89. width: 100%;
  90. height: 400px;
  91. }
  92. .search {
  93. padding: 10px;
  94. background-color: #fff;
  95. position: fixed;
  96. top: 44;
  97. left: 0;
  98. right: 0;
  99. z-index: 99;
  100. }
  101. .region {
  102. width: 70px;
  103. height: 40px;
  104. background-color: #f2f2f2;
  105. padding: 5px 10px;
  106. border-bottom-left-radius: 50px;
  107. border-top-left-radius: 50px;
  108. }
  109. .addressContent {
  110. padding: 20px;
  111. width: 100%;
  112. height: auto;
  113. background-color: #fff;
  114. position: absolute;
  115. top: 60px;
  116. view {
  117. font-size: 12px;
  118. }
  119. }
  120. </style>