安装
git地址
https://dafrok.github.io/vue-baidu-map/#/zh/guide/painting
$ npm install vue-baidu-map --save
自定义地图组件
需求
用户拖动红色标记点可以获得定位的点和地址,也可以通过搜索获得想要的地址,传给父组件
方案一
所有组件按需引入,但是发现有时候会报错误:Maximum call stack size exceeded,还是选择方案二(全局引入)
创建子组件
<template><div class="baidu-map"><div class="container" id="container"><!--查询结果--><h3><p>定位地址为:{{location}}({{map.center.lng}},{{map.center.lat}})</p></h3><!-- 这里放地图 --><div class="map-context"><baidu-mapclass="map-box"ak="your key":center="{lng: map.center.lng, lat: map.center.lat}":scroll-wheel-zoom="true"@click="getClickInfo"@ready="handler"><bm-view class="map-view"></bm-view><div class="map-search"><!--自定义搜索框--><div class="search-content"><a-input v-model="keyword" placeholder="输入地址进行搜索" style="width: 500px" /></div><!--搜索控件--><bm-local-searchclass="map-search-list":keyword="keyword":auto-viewport="true":location="location"@infohtmlset="infohtmlset"></bm-local-search></div><!-- 定位 --><!-- <bm-geolocationanchor="BMAP_ANCHOR_BOTTOM_RIGHT":showAddressBar="true":autoLocation="true"@locationSuccess="locationSuccess"@locationError="locationError"></bm-geolocation>--><!--动态添加的点坐标--><bm-marker-clusterer :averageCenter="true"><bm-marker:key="local":position="{lng: center.lng, lat: center.lat}":dragging="true"@dragend="dragend"></bm-marker></bm-marker-clusterer><!--缩放控件--><bm-navigation anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-navigation></baidu-map></div></div></div></template><script>//百度地图import BmView from 'vue-baidu-map/components/map/MapView'import BmLocalSearch from 'vue-baidu-map/components/search/LocalSearch'import BmGeolocation from 'vue-baidu-map/components/controls/Geolocation'import BaiduMap from 'vue-baidu-map/components/map/Map.vue'import BmScale from 'vue-baidu-map/components/controls/Scale'import BmNavigation from 'vue-baidu-map/components/controls/Navigation'import BmMarkerClusterer from 'vue-baidu-map/components/extra/MarkerClusterer'import BmMarker from 'vue-baidu-map/components/overlays/Marker'import BmInfoWindow from 'vue-baidu-map/components/overlays/InfoWindow'export default {name: 'baidu-map',components: {BmView,BmLocalSearch,BaiduMap,BmScale,BmNavigation,BmMarkerClusterer,BmMarker,BmInfoWindow,BmGeolocation},props: {local: {//默认地址type: String,default: ''},center: {//默认经纬度(中心)type: Object,default() {return {lat: 30.051208,lng: 120.583321}}}},watch: {local(val) {this.keyword = val},center(val) {this.map.center = val}},data() {return {BMap: {},location: '', // 地址keyword: '', //搜索关键词map: {center: { lng: 120.583321, lat: 30.051208 }, //'绍兴市'zoom: 12}}},methods: {/** ** 地图点击事件。*/getClickInfo(e) {// 调整地图中心位置this.center.lng = e.point.lngthis.center.lat = e.point.lat// 此时已经可以获取到BMap类if (this.BMap) {const that = this// Geocoder() 类进行地址解析// 创建地址解析器的实例const geoCoder = new this.BMap.Geocoder()// getLocation() 类--利用坐标获取地址的详细信息// getPoint() 类--获取位置对应的坐标geoCoder.getLocation(e.point, function(res) {console.log('获取经纬度', e.point, '获取详细地址', res)that.map.center = e.pointthat.location = res.addressthat.$emit('result', e.point)})}},// 初始化handler({ BMap, map }) {this.BMap = BMapthis.keyword = this.localif (this.BMap) {const that = this// Geocoder() 类进行地址解析// 创建地址解析器的实例const geoCoder = new this.BMap.Geocoder()// getLocation() 类--利用坐标获取地址的详细信息// getPoint() 类--获取位置对应的坐标geoCoder.getLocation(that.map.center, function(res) {console.log('获取经纬度', that.map.center, '获取详细地址', res)that.location = res.address ? res.address : ''})}},// 选择搜索的点infohtmlset(poi) {this.location = poi.titlethis.map.center = poi.pointthis.$emit('result', poi.point)},// 拖拽标记点出触发事件dragend({ type, target }) {console.log('拖拽标记点出触发事件', type, target)this.map.center = target.pointthis.$emit('result', target.point)if (this.BMap) {const that = this// Geocoder() 类进行地址解析// 创建地址解析器的实例const geoCoder = new this.BMap.Geocoder()// getLocation() 类--利用坐标获取地址的详细信息// getPoint() 类--获取位置对应的坐标geoCoder.getLocation(target.point, function(res) {console.log('获取经纬度', target.point, '获取详细地址', res)that.location = res.address})}}}}</script><style lang="less" scoped>.container {margin: 0 auto;width: 1366px;padding: 0px;min-height: 550px;.search-content {width: 100%;}.map-context {width: 950px;display: flex;justify-content: space-between;position: relative;margin-top: 10px;}.map-view {width: 800px;height: 550px;background: white;border-radius: 5px;margin-left: 10px;position: absolute;top: 0;right: 0;left: 500px;.right {text-align: left;}.left {width: 100px;}}.map-search {position: absolute;top: 0;left: 0;}.map-search-list {width: 500px;height: 490px;overflow: scroll;margin-top: 5px;}}</style>
父组件内的使用方法
<template><BaiduMap:center="{lat: location.latitude, lng: location.longitude}":local="location.address"@result="getLocaltion"></BaiduMap></template><script>import BaiduMap from '@/Template/BaiduMap'export default {name: 'Area',components: {BaiduMap},data() {return {location: {longitude: null,latitude: null,address: ''}}},methods: {/* 获取地图地址 */getLocaltion(local) {console.log('地图信息', local)this.location = {latitude: local.location.lat,longitude: local.location.lng,address: local.location.address}},}}</script>
方案二
全局引入
// 百度地图import BaiduMap from 'vue-baidu-map'Vue.use(BaiduMap, {/* Visit http://lbsyun.baidu.com/apiconsole/key for details about app key. */ak: 'your key'})
创建子组件
<template><baidu-map@ready="handler":scroll-wheel-zoom="true":center="{lng: center.lng, lat: center.lat}":zoom="zoom"@click="getClickInfo"><bm-view class="map"></bm-view><bm-control :offset="{width: '10px', height: '10px'}"><bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 1}"><a-input v-model="keyword" placeholder="请输入地名关键字" class="search-int" @change="changeInt" /><bm-local-searchv-if="isShow"class="result-box":keyword="keyword":auto-viewport="true"@infohtmlset="infohtmlset"></bm-local-search></bm-auto-complete></bm-control><!-- 缩放比例尺的展示 --><bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation><!-- 添加一个小红点的,并将当前的经纬度写入数据中 --><bm-marker :position="{lng:center.lng, lat: center.lat}"></bm-marker></baidu-map></template><script>export default {props: {local: {type: String,default: ''},mapcenter: {//默认经纬度(中心)type: Object,default() {return {lat: 30.051208,lng: 120.583321}}}},data() {return {BMap: {},isShow: false,keyword: '',resultList: [],containHtml: '',zoom: 12,center: { lng: 120.583321, lat: 30.051208 }}},watch: {local(val) {console.log('搜索-----', val)this.keyword = val},mapcenter(val) {console.log('center-----', val)this.center = val}},mounted(){console.log('传递过来的地址',this.local)this.keyword = this.local},methods: {// 初始化handler: function({ BMap, map }) {map.enableScrollWheelZoom(true)const _this = thisconst geolocation = new BMap.Geolocation()geolocation.getCurrentPosition(function(r) {_this.center = { lng: r.longitude, lat: r.latitude } // 设置center属性值},{ enableHighAccuracy: true })window.map = map// 赋值,方便调用,本节被用到this.BMap = BMap},// 输入框输入changeInt(){this.isShow = true},//点击获取到当前经纬度getClickInfo(e) {console.log('点击获取', e)const _this = this_this.center.lng = e.point.lng_this.center.lat = e.point.latconst gc = new this.BMap.Geocoder()gc.getLocation(e.point, function(rs) {console.log('点击获取到当前经纬度', rs)_this.$emit('result', rs)})_this.zoom = e.target.getZoom()},// 标注气泡内容创建后的回调函数infohtmlset(poi) {console.log('标注气泡内容创建后的回调函数', poi)this.isShow = falsethis.keyword = poi.title + '(' + poi.address + ')'this.center = poi.pointthis.$emit('result', poi)}}}</script><style lang="less" scoped>.map {width: 100%;height: 500px;}.search-int {width: 100%;min-width: 548px;height: 40px;position: relative;}.result-box {width: 100%;min-width: 548px;position: absolute;top: 40px;left: 0px;}</style>
