main.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div>
  3. <baidu-map v-bind:style="mapStyle" class="bm-view" :ak="akey"
  4. :center="center"
  5. :zoom="zoom"
  6. :scroll-wheel-zoom="true"
  7. @click="getClickInfo"
  8. @moving="syncCenterAndZoom"
  9. @moveend="syncCenterAndZoom"
  10. @zoomend="syncCenterAndZoom">
  11. <bm-view style="width: 100%; height:100%;"></bm-view>
  12. <bm-marker v-if="positionUrl" :position="{lng: center.lng, lat: center.lat}" :dragging="true"
  13. animation="BMAP_ANIMATION_BOUNCE" :icon="{ url: positionUrl, size: { width: 54, height: 54 }}"></bm-marker>
  14. <bm-marker v-else :position="{lng: center.lng, lat: center.lat}" :dragging="true"
  15. animation="BMAP_ANIMATION_BOUNCE"></bm-marker>
  16. <bm-control :offset="{width: '10px', height: '10px'}">
  17. <bm-auto-complete v-model="address" :sugStyle="{zIndex: 999999}">
  18. <input type="text" v-model="address" placeholder="请输入搜索关键字" class="serachinput">
  19. </bm-auto-complete>
  20. </bm-control>
  21. <bm-local-search :keyword="address" :auto-viewport="true" style="width:0px;height:0px;overflow: hidden;"></bm-local-search>
  22. </baidu-map>
  23. <div class="footer">
  24. <div class="nt-info nt-btn-bottom" @click.stop="mapCancel">取消</div>
  25. <div class="nt-primary nt-btn-bottom" @click.prevent.stop="mapConfirm">确定</div>
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. import {BaiduMap, BmControl, BmView, BmAutoComplete, BmLocalSearch, BmMarker} from 'vue-baidu-map'
  31. export default {
  32. name: 'NtMap',
  33. components: {
  34. BaiduMap,
  35. BmControl,
  36. BmView,
  37. BmAutoComplete,
  38. BmLocalSearch,
  39. BmMarker
  40. },
  41. data: function () {
  42. return {
  43. address: this.addressKey,
  44. mapStyle: {
  45. width: '100%',
  46. overflow: 'hidden',
  47. minHeight: '280px',
  48. height: this.mapHeight
  49. },
  50. center: {lng: 116.404, lat: 39.915},
  51. zoom: 15
  52. }
  53. },
  54. watch: {},
  55. props: {
  56. akey: {
  57. type: String,
  58. default: 'dfhycORtYDMz78dNLo9oNiDO1ufI2TZS'
  59. },
  60. addressKey: {
  61. type: String,
  62. default: ''
  63. },
  64. pointValue: {
  65. type: String,
  66. default: ''
  67. },
  68. inputItem: {
  69. type: Object
  70. },
  71. mapHeight: {
  72. type: String,
  73. default: '280px'
  74. },
  75. positionUrl: {
  76. type: String
  77. }
  78. },
  79. created() {
  80. this.initData()
  81. },
  82. methods: {
  83. initData() {
  84. if (this.pointValue) {
  85. const points = this.pointValue.split(((this.inputItem && this.inputItem.sign) || ','));
  86. this.address = '';
  87. this.center = {
  88. lng: points[0] || 116.404,
  89. lat: points[1] || 39.915
  90. }
  91. }
  92. },
  93. /***
  94. * 地图点击事件。
  95. */
  96. getClickInfo(e) {
  97. this.center.lng = e.point.lng
  98. this.center.lat = e.point.lat
  99. },
  100. syncCenterAndZoom(e) {
  101. const {lng, lat} = e.target.getCenter()
  102. this.center.lng = lng
  103. this.center.lat = lat
  104. this.zoom = e.target.getZoom()
  105. },
  106. /***确认*/
  107. mapConfirm: function () {
  108. this.$emit('mapConfirm', this.center)
  109. },
  110. /***取消*/
  111. mapCancel: function () {
  112. this.$emit('mapCancel')
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. .serachinput {
  119. width: 200px;
  120. box-sizing: border-box;
  121. padding: 9px;
  122. border: 1px solid #dddee1;
  123. line-height: 12px;
  124. font-size: 12px;
  125. height: 30px;
  126. color: #333;
  127. outline-width: 0;
  128. outline-color: rgba(0, 0, 0, 0);
  129. position: relative;
  130. border-radius: 4px;
  131. -webkit-box-shadow: #666 0px 0px 10px;
  132. -moz-box-shadow: #666 0px 0px 10px;
  133. box-shadow: #666 0px 0px 10px;
  134. }
  135. .footer {
  136. padding-top: 10px;
  137. text-align: center;
  138. display: flex;
  139. justify-content: flex-end;
  140. align-items: center;
  141. .nt-btn-bottom {
  142. width: 60px;
  143. height: 28px;
  144. line-height: 25px;
  145. margin-left: 10px;
  146. cursor: pointer;
  147. &.nt-info {
  148. border: 1px solid #e2e2e2;
  149. background-color: #f8f8f8;
  150. &:hover {
  151. background-color: rgba(248, 248, 248, .5)
  152. }
  153. }
  154. &.nt-primary {
  155. color: #ffffff;
  156. border: 1px solid #409EFF;
  157. background-color: #409EFF;
  158. &:hover {
  159. background-color: rgba(64, 158, 255, .85)
  160. }
  161. }
  162. }
  163. }
  164. </style>