安装

git地址
https://dafrok.github.io/vue-baidu-map/#/zh/guide/painting

  1. $ npm install vue-baidu-map --save

自定义地图组件

需求

用户拖动红色标记点可以获得定位的点和地址,也可以通过搜索获得想要的地址,传给父组件

方案一

所有组件按需引入,但是发现有时候会报错误:Maximum call stack size exceeded,还是选择方案二(全局引入)

创建子组件

  1. <template>
  2. <div class="baidu-map">
  3. <div class="container" id="container">
  4. <!--查询结果-->
  5. <h3>
  6. <p>定位地址为:{{location}}({{map.center.lng}},{{map.center.lat}})</p>
  7. </h3>
  8. <!-- 这里放地图 -->
  9. <div class="map-context">
  10. <baidu-map
  11. class="map-box"
  12. ak="your key"
  13. :center="{lng: map.center.lng, lat: map.center.lat}"
  14. :scroll-wheel-zoom="true"
  15. @click="getClickInfo"
  16. @ready="handler"
  17. >
  18. <bm-view class="map-view"></bm-view>
  19. <div class="map-search">
  20. <!--自定义搜索框-->
  21. <div class="search-content">
  22. <a-input v-model="keyword" placeholder="输入地址进行搜索" style="width: 500px" />
  23. </div>
  24. <!--搜索控件-->
  25. <bm-local-search
  26. class="map-search-list"
  27. :keyword="keyword"
  28. :auto-viewport="true"
  29. :location="location"
  30. @infohtmlset="infohtmlset"
  31. ></bm-local-search>
  32. </div>
  33. <!-- 定位 -->
  34. <!-- <bm-geolocation
  35. anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
  36. :showAddressBar="true"
  37. :autoLocation="true"
  38. @locationSuccess="locationSuccess"
  39. @locationError="locationError"
  40. ></bm-geolocation>-->
  41. <!--动态添加的点坐标-->
  42. <bm-marker-clusterer :averageCenter="true">
  43. <bm-marker
  44. :key="local"
  45. :position="{lng: center.lng, lat: center.lat}"
  46. :dragging="true"
  47. @dragend="dragend"
  48. ></bm-marker>
  49. </bm-marker-clusterer>
  50. <!--缩放控件-->
  51. <bm-navigation anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-navigation>
  52. </baidu-map>
  53. </div>
  54. </div>
  55. </div>
  56. </template>
  57. <script>
  58. //百度地图
  59. import BmView from 'vue-baidu-map/components/map/MapView'
  60. import BmLocalSearch from 'vue-baidu-map/components/search/LocalSearch'
  61. import BmGeolocation from 'vue-baidu-map/components/controls/Geolocation'
  62. import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
  63. import BmScale from 'vue-baidu-map/components/controls/Scale'
  64. import BmNavigation from 'vue-baidu-map/components/controls/Navigation'
  65. import BmMarkerClusterer from 'vue-baidu-map/components/extra/MarkerClusterer'
  66. import BmMarker from 'vue-baidu-map/components/overlays/Marker'
  67. import BmInfoWindow from 'vue-baidu-map/components/overlays/InfoWindow'
  68. export default {
  69. name: 'baidu-map',
  70. components: {
  71. BmView,
  72. BmLocalSearch,
  73. BaiduMap,
  74. BmScale,
  75. BmNavigation,
  76. BmMarkerClusterer,
  77. BmMarker,
  78. BmInfoWindow,
  79. BmGeolocation
  80. },
  81. props: {
  82. local: {
  83. //默认地址
  84. type: String,
  85. default: ''
  86. },
  87. center: {
  88. //默认经纬度(中心)
  89. type: Object,
  90. default() {
  91. return {
  92. lat: 30.051208,
  93. lng: 120.583321
  94. }
  95. }
  96. }
  97. },
  98. watch: {
  99. local(val) {
  100. this.keyword = val
  101. },
  102. center(val) {
  103. this.map.center = val
  104. }
  105. },
  106. data() {
  107. return {
  108. BMap: {},
  109. location: '', // 地址
  110. keyword: '', //搜索关键词
  111. map: {
  112. center: { lng: 120.583321, lat: 30.051208 }, //'绍兴市'
  113. zoom: 12
  114. }
  115. }
  116. },
  117. methods: {
  118. /** *
  119. * 地图点击事件。
  120. */
  121. getClickInfo(e) {
  122. // 调整地图中心位置
  123. this.center.lng = e.point.lng
  124. this.center.lat = e.point.lat
  125. // 此时已经可以获取到BMap类
  126. if (this.BMap) {
  127. const that = this
  128. // Geocoder() 类进行地址解析
  129. // 创建地址解析器的实例
  130. const geoCoder = new this.BMap.Geocoder()
  131. // getLocation() 类--利用坐标获取地址的详细信息
  132. // getPoint() 类--获取位置对应的坐标
  133. geoCoder.getLocation(e.point, function(res) {
  134. console.log('获取经纬度', e.point, '获取详细地址', res)
  135. that.map.center = e.point
  136. that.location = res.address
  137. that.$emit('result', e.point)
  138. })
  139. }
  140. },
  141. // 初始化
  142. handler({ BMap, map }) {
  143. this.BMap = BMap
  144. this.keyword = this.local
  145. if (this.BMap) {
  146. const that = this
  147. // Geocoder() 类进行地址解析
  148. // 创建地址解析器的实例
  149. const geoCoder = new this.BMap.Geocoder()
  150. // getLocation() 类--利用坐标获取地址的详细信息
  151. // getPoint() 类--获取位置对应的坐标
  152. geoCoder.getLocation(that.map.center, function(res) {
  153. console.log('获取经纬度', that.map.center, '获取详细地址', res)
  154. that.location = res.address ? res.address : ''
  155. })
  156. }
  157. },
  158. // 选择搜索的点
  159. infohtmlset(poi) {
  160. this.location = poi.title
  161. this.map.center = poi.point
  162. this.$emit('result', poi.point)
  163. },
  164. // 拖拽标记点出触发事件
  165. dragend({ type, target }) {
  166. console.log('拖拽标记点出触发事件', type, target)
  167. this.map.center = target.point
  168. this.$emit('result', target.point)
  169. if (this.BMap) {
  170. const that = this
  171. // Geocoder() 类进行地址解析
  172. // 创建地址解析器的实例
  173. const geoCoder = new this.BMap.Geocoder()
  174. // getLocation() 类--利用坐标获取地址的详细信息
  175. // getPoint() 类--获取位置对应的坐标
  176. geoCoder.getLocation(target.point, function(res) {
  177. console.log('获取经纬度', target.point, '获取详细地址', res)
  178. that.location = res.address
  179. })
  180. }
  181. }
  182. }
  183. }
  184. </script>
  185. <style lang="less" scoped>
  186. .container {
  187. margin: 0 auto;
  188. width: 1366px;
  189. padding: 0px;
  190. min-height: 550px;
  191. .search-content {
  192. width: 100%;
  193. }
  194. .map-context {
  195. width: 950px;
  196. display: flex;
  197. justify-content: space-between;
  198. position: relative;
  199. margin-top: 10px;
  200. }
  201. .map-view {
  202. width: 800px;
  203. height: 550px;
  204. background: white;
  205. border-radius: 5px;
  206. margin-left: 10px;
  207. position: absolute;
  208. top: 0;
  209. right: 0;
  210. left: 500px;
  211. .right {
  212. text-align: left;
  213. }
  214. .left {
  215. width: 100px;
  216. }
  217. }
  218. .map-search {
  219. position: absolute;
  220. top: 0;
  221. left: 0;
  222. }
  223. .map-search-list {
  224. width: 500px;
  225. height: 490px;
  226. overflow: scroll;
  227. margin-top: 5px;
  228. }
  229. }
  230. </style>

父组件内的使用方法

  1. <template>
  2. <BaiduMap
  3. :center="{lat: location.latitude, lng: location.longitude}"
  4. :local="location.address"
  5. @result="getLocaltion"
  6. ></BaiduMap>
  7. </template>
  8. <script>
  9. import BaiduMap from '@/Template/BaiduMap'
  10. export default {
  11. name: 'Area',
  12. components: {
  13. BaiduMap
  14. },
  15. data() {
  16. return {
  17. location: {
  18. longitude: null,
  19. latitude: null,
  20. address: ''
  21. }
  22. }
  23. },
  24. methods: {
  25. /* 获取地图地址 */
  26. getLocaltion(local) {
  27. console.log('地图信息', local)
  28. this.location = {
  29. latitude: local.location.lat,
  30. longitude: local.location.lng,
  31. address: local.location.address
  32. }
  33. },
  34. }
  35. }
  36. </script>

方案二

全局引入

  1. // 百度地图
  2. import BaiduMap from 'vue-baidu-map'
  3. Vue.use(BaiduMap, {
  4. /* Visit http://lbsyun.baidu.com/apiconsole/key for details about app key. */
  5. ak: 'your key'
  6. })

创建子组件

  1. <template>
  2. <baidu-map
  3. @ready="handler"
  4. :scroll-wheel-zoom="true"
  5. :center="{lng: center.lng, lat: center.lat}"
  6. :zoom="zoom"
  7. @click="getClickInfo"
  8. >
  9. <bm-view class="map"></bm-view>
  10. <bm-control :offset="{width: '10px', height: '10px'}">
  11. <bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 1}">
  12. <a-input v-model="keyword" placeholder="请输入地名关键字" class="search-int" @change="changeInt" />
  13. <bm-local-search
  14. v-if="isShow"
  15. class="result-box"
  16. :keyword="keyword"
  17. :auto-viewport="true"
  18. @infohtmlset="infohtmlset"
  19. ></bm-local-search>
  20. </bm-auto-complete>
  21. </bm-control>
  22. <!-- 缩放比例尺的展示 -->
  23. <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
  24. <!-- 添加一个小红点的,并将当前的经纬度写入数据中 -->
  25. <bm-marker :position="{lng:center.lng, lat: center.lat}"></bm-marker>
  26. </baidu-map>
  27. </template>
  28. <script>
  29. export default {
  30. props: {
  31. local: {
  32. type: String,
  33. default: ''
  34. },
  35. mapcenter: {
  36. //默认经纬度(中心)
  37. type: Object,
  38. default() {
  39. return {
  40. lat: 30.051208,
  41. lng: 120.583321
  42. }
  43. }
  44. }
  45. },
  46. data() {
  47. return {
  48. BMap: {},
  49. isShow: false,
  50. keyword: '',
  51. resultList: [],
  52. containHtml: '',
  53. zoom: 12,
  54. center: { lng: 120.583321, lat: 30.051208 }
  55. }
  56. },
  57. watch: {
  58. local(val) {
  59. console.log('搜索-----', val)
  60. this.keyword = val
  61. },
  62. mapcenter(val) {
  63. console.log('center-----', val)
  64. this.center = val
  65. }
  66. },
  67. mounted(){
  68. console.log('传递过来的地址',this.local)
  69. this.keyword = this.local
  70. },
  71. methods: {
  72. // 初始化
  73. handler: function({ BMap, map }) {
  74. map.enableScrollWheelZoom(true)
  75. const _this = this
  76. const geolocation = new BMap.Geolocation()
  77. geolocation.getCurrentPosition(
  78. function(r) {
  79. _this.center = { lng: r.longitude, lat: r.latitude } // 设置center属性值
  80. },
  81. { enableHighAccuracy: true }
  82. )
  83. window.map = map
  84. // 赋值,方便调用,本节被用到
  85. this.BMap = BMap
  86. },
  87. // 输入框输入
  88. changeInt(){
  89. this.isShow = true
  90. },
  91. //点击获取到当前经纬度
  92. getClickInfo(e) {
  93. console.log('点击获取', e)
  94. const _this = this
  95. _this.center.lng = e.point.lng
  96. _this.center.lat = e.point.lat
  97. const gc = new this.BMap.Geocoder()
  98. gc.getLocation(e.point, function(rs) {
  99. console.log('点击获取到当前经纬度', rs)
  100. _this.$emit('result', rs)
  101. })
  102. _this.zoom = e.target.getZoom()
  103. },
  104. // 标注气泡内容创建后的回调函数
  105. infohtmlset(poi) {
  106. console.log('标注气泡内容创建后的回调函数', poi)
  107. this.isShow = false
  108. this.keyword = poi.title + '(' + poi.address + ')'
  109. this.center = poi.point
  110. this.$emit('result', poi)
  111. }
  112. }
  113. }
  114. </script>
  115. <style lang="less" scoped>
  116. .map {
  117. width: 100%;
  118. height: 500px;
  119. }
  120. .search-int {
  121. width: 100%;
  122. min-width: 548px;
  123. height: 40px;
  124. position: relative;
  125. }
  126. .result-box {
  127. width: 100%;
  128. min-width: 548px;
  129. position: absolute;
  130. top: 40px;
  131. left: 0px;
  132. }
  133. </style>

父组件使用方式同方案一