// 初始化
reset () {
const {getMapScript, initMap} = this
getMapScript().then(initMap)
},
// 获取baidumap
getMapScript () {
if (!global.BMap) {
const ak = this.ak || this._BMap().ak
global.BMap = {}
global.BMap._preloader = new Promise((resolve, reject) => {
global._initBaiduMap = function () {
resolve(global.BMap)
global.document.body.removeChild($script)
global.BMap._preloader = null
global._initBaiduMap = null
}
const $script = document.createElement('script')
global.document.body.appendChild($script)
$script.src = `https://api.map.baidu.com/api?v=2.0&ak=${ak}&callback=_initBaiduMap`
})
return global.BMap._preloader
} else if (!global.BMap._preloader) {
return Promise.resolve(global.BMap)
} else {
return global.BMap._preloader
}
},
// 获取BMap, 初始化地图
initMap (BMap) {
this.BMap = BMap
this.init(BMap)
},
init (BMap) {
let $el = this.$refs.basicMap
const map = new BMap.Map($el)
this.map = map
this.setMapOptions()
map.centerAndZoom(this.initCenter, this.initZoom)
this.$emit('ready', {BMap, map})
},
// 设置地图配置
setMapOptions () {
}
效果如图:
baiduMap.vue
this.$emit('ready', {BMap, map})
业务组件
initReady ({BMap, map}) {
let point = new BMap.Point(116.404, 39.915)
map.centerAndZoom(point, 15)
let marker = new BMap.Marker(point)
map.addOverlay(marker)
}
效果图:
map.centerAndZoom(new BMap.Point(116.404, 39.915), 15)
let myP1 = new BMap.Point(116.380967, 39.913285)
let myP2 = new BMap.Point(116.424374, 39.914668)
let driving = new BMap.DrivingRoute(map, {renderOptions: {map: map, autoViewport: true}})
driving.search(myP1, myP2)
效果图:
---------- map.centerAndZoom(new BMap.Point(116.404, 39.915), 15)
map.enableScrollWheelZoom()
// 创建多边形
let polygon = new BMap.Polygon([
new BMap.Point(116.387112, 39.920977),
new BMap.Point(116.385243, 39.913063),
new BMap.Point(116.394226, 39.917988),
new BMap.Point(116.401772, 39.921364),
new BMap.Point(116.41248, 39.927893)
], {strokeColor: 'blue', strokeWeight: 2, strokeOpacity: 0.5})
// 增加多边形
map.addOverlay(polygon)
polygon.enableEditing()
效果图:
---------- 待续 ----------
完整项目地址: https://github.com/hty7/vue-demo.git
下一篇文章:vue 地图可视化(2)Mapbox地图篇