MapSelect.vue 4.0 KB

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