高德地图常用方法封装

在开发中,经常用到高德地图,于是总结了一些常用的方法进行封装

动态创建高德脚本

创建 createAMap.js

import { config } from '../config'
import Bus from '../eventBus.js'
class CreateAMap {
	constructor() {
		this.init()
	}
	init () {
		window.initAMap = () => {
			Bus.$emit('AMapIsExist', null)
		}
		let plugins = 'AMap.Driving,AMap.Geocoder,AMap.Walking,AMap.Riding,AMap.Transfer,AMap.Geolocation'
		let script = document.createElement('script')
		script.type = 'text/javascript'
		script.async = true
		script.src = `https://webapi.amap.com/maps?v=1.4.15&callback=initAMap&key=${config.amapKey}&plugin=${plugins}`
		// script.onerror = reject
		document.head.appendChild(script)
	}
}
let createAMap = new CreateAMap()
export { createAMap }

vue 页面使用

封装高德常用的方法

amap.js 如下:

amap 挂在 util 对象下,方便调用

参考

Last updated

Was this helpful?