chooseAddress.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <view class="uni-app">
  3. <view class="status-bar" />
  4. <view class="main-container">
  5. <wk-nav-bar title="选择位置" pervent @back="handleCancel">
  6. <!-- #ifndef MP-WEIXIN -->
  7. <!-- <button class="button white-btn" @click="handleConfirm">
  8. 确定
  9. </button> -->
  10. <!-- #endif -->
  11. </wk-nav-bar>
  12. <view class="container">
  13. <map
  14. id="chooseMap"
  15. :style="{
  16. width: mapStyle.width,
  17. flex: mapStyle.flex
  18. }"
  19. :longitude="centerPoint.longitude"
  20. :latitude="centerPoint.latitude"
  21. :markers="markers" />
  22. <view class="poi-container">
  23. <view
  24. v-for="(poi, index) in poiList"
  25. :key="poi.id"
  26. class="poi-item"
  27. @click="handleChoosePoi(index, $event)">
  28. <view class="left">
  29. <view class="poi-item-name">
  30. {{ poi.name }}
  31. </view>
  32. <view class="poi-item-address">
  33. {{ poi.address || '' }}
  34. </view>
  35. </view>
  36. <view class="right">
  37. <text v-if="activeIndex === index" class="wk wk-check icon" />
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import {
  47. getLocation,
  48. getNearbyPois
  49. } from '@/utils/map.js'
  50. import { deepCopy } from '@/utils/lib.js'
  51. export default {
  52. name: 'ChooseAddress',
  53. data() {
  54. return {
  55. centerPoint: {}, // 地图中心
  56. devicePoint: {}, // 设备地址
  57. poiList: [], // poi
  58. activeIndex: 0,
  59. mapStyle: {
  60. width: 0,
  61. flex: 1
  62. },
  63. markers: []
  64. }
  65. },
  66. mounted() {
  67. this.mapStyle = {
  68. width: uni.upx2px(750) + 'px',
  69. flex: 1
  70. }
  71. this.getLocation()
  72. },
  73. methods: {
  74. /**
  75. * 定位
  76. */
  77. getLocation() {
  78. getLocation(false).then(data => {
  79. console.log('定位结果:0.0', data)
  80. this.centerPoint = data
  81. this.devicePoint = deepCopy(this.centerPoint)
  82. this.getNearbyPois(data)
  83. }).catch(() => {
  84. this.$toast('定位失败')
  85. })
  86. },
  87. /**
  88. * 逆地址解析(获取附近的POI点)
  89. */
  90. getNearbyPois(data) {
  91. // >> 高德地图 <<
  92. // getNearbyPois({
  93. // location: `${data.longitude},${data.latitude}`,
  94. // extensions: 'all',
  95. // output: 'json'
  96. // }).then(data => {
  97. // console.log('位置信息: ', data)
  98. // this.centerPoint.address = data.regeocode.formatted_address
  99. // if (this.$isEmpty(this.centerPoint.address)) {
  100. // this.centerPoint.address = ''
  101. // }
  102. // this.devicePoint = deepCopy(this.centerPoint)
  103. // this.poiList = data.regeocode.pois.map(o => {
  104. // return {
  105. // ...o,
  106. // longitude: o.location.split(',')[0],
  107. // latitude: o.location.split(',')[1],
  108. // }
  109. // })
  110. // if (this.poiList.length > 0) {
  111. // this.poiList.unshift({
  112. // id: 1,
  113. // name: '我的位置',
  114. // address: this.centerPoint.address,
  115. // longitude: this.devicePoint.longitude,
  116. // latitude: this.devicePoint.latitude
  117. // })
  118. // this.handleChoosePoi(0)
  119. // } else {
  120. // this.markers = [this.centerPoint]
  121. // }
  122. // }).catch(() => {
  123. // this.$toast('获取位置信息失败')
  124. // })
  125. // >> 腾讯地图 <<
  126. getNearbyPois({
  127. location: `${data.latitude},${data.longitude}`,
  128. get_poi: 1,
  129. poi_options: 'radius=200'
  130. }).then(res => {
  131. console.log('位置信息: ', res)
  132. this.centerPoint.address = res.result.address
  133. if (this.$isEmpty(this.centerPoint.address)) {
  134. this.centerPoint.address = ''
  135. }
  136. this.devicePoint = deepCopy(this.centerPoint)
  137. this.poiList = res.result.pois.map(o => {
  138. return {
  139. ...o,
  140. name: o.title,
  141. longitude: o.location.lng,
  142. latitude: o.location.lat,
  143. }
  144. })
  145. if (this.poiList.length > 0) {
  146. this.poiList.unshift({
  147. id: 1,
  148. name: '我的位置',
  149. address: this.centerPoint.address,
  150. longitude: this.devicePoint.longitude,
  151. latitude: this.devicePoint.latitude
  152. })
  153. } else {
  154. this.markers = [this.centerPoint]
  155. }
  156. }).catch(() => {
  157. this.$toast('获取位置信息失败')
  158. })
  159. // -----------------------------------------------------------------
  160. },
  161. handleChoosePoi(index, evt) {
  162. this.activeIndex = index
  163. this.centerPoint = this.poiList[index]
  164. this.markers = [{
  165. ...this.centerPoint,
  166. width: 30,
  167. height: 30,
  168. iconPath: this.$static('images/map/location.png')
  169. }]
  170. if (evt) {
  171. this.handleConfirm()
  172. }
  173. },
  174. handleConfirm() {
  175. uni.$emit('choose-address', this.centerPoint)
  176. this.$Router.navigateBack()
  177. },
  178. handleCancel() {
  179. uni.$off('choose-address')
  180. this.$Router.navigateBack()
  181. }
  182. }
  183. }
  184. </script>
  185. <style scoped lang="scss">
  186. .container {
  187. flex: 1;
  188. width: 100%;
  189. display: flex;
  190. flex-direction: column;
  191. overflow: hidden;
  192. #chooseMap {
  193. width: 750rpx;
  194. }
  195. .poi-container {
  196. width: 100%;
  197. max-height: 450rpx;
  198. overflow: auto;
  199. background-color: white;
  200. .poi-item {
  201. padding: 15rpx 32rpx;
  202. border-bottom: 1rpx solid $border-color;
  203. @include left;
  204. .left {
  205. flex: 1;
  206. overflow: hidden;
  207. .poi-item-name {
  208. font-size: 30rpx;
  209. @include ellipsis;
  210. }
  211. .poi-item-address {
  212. font-size: 24rpx;
  213. color: $light;
  214. @include ellipsis;
  215. }
  216. }
  217. .right {
  218. width: 50rpx;
  219. margin-left: 20rpx;
  220. @include center;
  221. .icon {
  222. font-size: 36rpx;
  223. color: $theme-color;
  224. }
  225. }
  226. }
  227. .poi-item::last-child {
  228. border-bottom: 0 none;
  229. }
  230. }
  231. }
  232. </style>