展示效果:
开始前可以先去申请一个百度地图秘钥,点击
1.创建一个loadBMap.js,调用百度地址api js
/**
* 动态加载百度地图api函数
* @param {String} ak 百度地图AK,必传
*/
export default function loadBMap(ak) {
return new Promise(function(resolve, reject) {
if (typeof window.BMap !== 'undefined') {
resolve(window.BMap)
return true
}
window.onBMapCallback = function() {
resolve(window.BMap)
}
let script = document.createElement('script')
script.type = 'text/javascript'
script.src =
'http://api.map.baidu.com/api?v=3.0&ak=' + ak + '&callback=onBMapCallback'
script.onerror = reject
document.head.appendChild(script)
})
}
2.创建组件
<template>
<div>
<el-form label-width="80px">
<el-row>
<el-col :span="24">
<el-form-item label="搜索地址">
<el-autocomplete
style="width:100%;"
popper-class="autoAddressClass"
v-model="addressInfo.address"
:fetch-suggestions="querySearchAsync"
:trigger-on-focus="false"
placeholder="详细地址"