MapSelect.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="div common-map-select">
  3. <view class="div search-wrap"><text class="i iconfont" @click="searchAddress">&#xe6cb;</text><input type="text" v-model="keyword"><view @click="searchAddress" class="div btn">搜索</view></view>
  4. <view class="div result-list" >
  5. <view class="div" :class="{'current':style}" v-if="member_point && style && ifShowCurrent">
  6. <text class="span notice">当前地址<text class="span reset" @click="getPosition"><text class="i iconfont">&#xe6d3;</text>重新定位</text></text>
  7. <view class="div">
  8. <view class="div result-item" style="border-bottom:0" @click="setPosition(member_point.lat,member_point.lng,member_point.address)">{{member_point.address}}</view>
  9. </view>
  10. </view>
  11. <view class="div" :class="{'nearby':style}">
  12. <text class="span notice">附近地址</text>
  13. <view class="div" v-for="(item,index) in address_list" :key="index">
  14. <view class="div result-item" v-if="item.location" @click="setPosition(item.location.lat,item.location.lng,item.name)">{{item.name}}</view>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import { getAddressByKeyword, getAddressByPoint, getPointNearby, getPosition } from '../util/bmap'
  22. import { mapState, mapMutations } from 'vuex'
  23. export default {
  24. name: 'MapSelect',
  25. data () {
  26. return {
  27. keyword: '',
  28. address_list: [],
  29. // wrapperHeight: 0,
  30. bmap: false,
  31. myMarker: false,
  32. cityCode: 0,
  33. style: false
  34. }
  35. },
  36. props: {
  37. longitude: {},
  38. latitude: {},
  39. ifShowCurrent:{
  40. type:Boolean,
  41. default:false
  42. },
  43. },
  44. watch: {
  45. longitude: function (newLongitude, oldLongitude) {
  46. if (newLongitude) {
  47. this.creatMap()
  48. }
  49. }
  50. },
  51. computed: {
  52. ...mapState({
  53. member_point: state => state.member.point
  54. })
  55. },
  56. mounted () {
  57. },
  58. created () {
  59. if (this.longitude) {
  60. this.creatMap()
  61. }
  62. },
  63. methods: {
  64. ...mapMutations({
  65. memberPoint: 'memberPoint'
  66. }),
  67. getPosition () {
  68. let _this = this
  69. uni.showLoading({ title: '加载中' })
  70. getPosition(function (res) {
  71. uni.hideLoading()
  72. if (res.code === 10000) {
  73. _this.getAddressByPoint(res.result.lat, res.result.lng)
  74. } else {
  75. uni.showToast({icon:'none',title: res.message})
  76. }
  77. }, true)
  78. },
  79. getAddressByPoint (lat, lng) {
  80. getAddressByPoint(lat + ',' + lng).then(res => {
  81. this.cityCode = res.result.cityCode
  82. this.memberPoint({ point: { lng: lng, lat: lat, address: res.result.sematic_description, cityCode: res.result.cityCode } })
  83. }).catch(function (error) {
  84. uni.showToast({icon:'none',title: error.message})
  85. })
  86. },
  87. creatMap () {
  88. this.getAddressByPoint(this.latitude, this.longitude)
  89. // 附近地址
  90. getPointNearby(this.latitude + ',' + this.longitude).then(res => {
  91. this.address_list = res.results
  92. this.style = true
  93. }).catch(function (error) {
  94. uni.showToast({icon:'none',title: error.message})
  95. })
  96. // let point = new BMap.Point(this.longitude, this.latitude)
  97. // 创建点坐标
  98. // this.bmap.centerAndZoom(point, 16)
  99. // this.myMarker = new BMap.Marker(point)
  100. // this.bmap.addOverlay(this.myMarker)
  101. },
  102. searchAddress () {
  103. getAddressByKeyword(this.keyword, this.cityCode).then(res => {
  104. this.address_list = res.result
  105. this.style = false
  106. }).catch(function (error) {
  107. uni.showToast({icon:'none',title: error.message})
  108. })
  109. },
  110. setPosition (lat, lng, name) {
  111. this.$emit('setPosition', lat, lng, name, this.cityCode)
  112. }
  113. }
  114. }
  115. </script>
  116. <style lang="scss" scoped>
  117. .common-map-select{background: #fff;position: absolute;width:100%;height:100%;
  118. .search-wrap{height: 2rem;line-height: 2rem;position: relative;position: absolute;left:.5rem;top:$pageSpace;right:$pageSpace;background: #F5F5F5;display: flex;border-radius:.4rem;overflow: hidden;
  119. input{width: 100%;height: 2rem;line-height: 2rem;font-size:$h2;display: block;background: none;border: 0;flex:1}
  120. .i{font-size:$h1;line-height: 2rem;display: block;width:2rem;text-align: center}
  121. .btn{background: $primaryColor;color:#fff;font-size:$subFontSize;padding:0 .5rem;}
  122. }
  123. .result-list{overflow-y:auto;position: absolute;top:3rem;bottom:0rem;width:100%;
  124. .notice{display: none;padding:1rem $pageSpace 0 $pageSpace;font-size:$subFontSize;color:#aeaeae;line-height: 1.5}
  125. .reset{float: right;color:$primaryColor}
  126. .current .notice,.nearby .notice{display: block}
  127. .current{box-shadow: 0px 4px 4px #f7f7f7;}
  128. .result-item{padding:.7rem 0;font-size:$h2;color:#333;border-bottom: 1px dashed #eee;margin:0 $pageSpace;}
  129. }
  130. }
  131. </style>