1. 安装npm install vue-baidu-map —save
2. pulic/index.html引入js文件
<head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=AoV4NBftZmNXf4LgCCmZ0CnrGCoOqHLE"></script>
<script type="text/javascript" src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=AoV4NBftZmNXf4LgCCmZ0CnrGCoOqHLE"></script>
</head>
3.在组件中使用
<div class="baidumap" id="allmap"></div>
<div class="transit">
<span style="font-weight: 800;color: #606266;">坐标经纬度</span>
<span style="margin-left: 5px;"> {{lng}}/{{lat}}</span>
</div>
<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
export defalut{
data(){
return:{
address_detail: null,
lng: 0,
lat: 0
}
},
mounted() {
this.getMap()
},
methods:{
getMap() {
var th = this
var map = new BMap.Map("allmap");
var point = new BMapGL.Point(105.291, 37.094);
map.centerAndZoom(point, 5);
map.enableScrollWheelZoom(true);
var ac = new BMap.Autocomplete(
{
"input": "suggestId",
"location": map
})
var myValue
ac.addEventListener("onconfirm", function(e) {
var _value = e.item.value;
myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
this.address_detail = myValue
setPlace();
});
function setPlace() {
map.clearOverlays();
function myFun() {
th.userlocation = local.getResults().getPoi(0).point;
map.centerAndZoom(th.userlocation, 18);
th.lng = th.userlocation.lng
th.lat = th.userlocation.lat
map.addOverlay(new BMap.Marker(th.userlocation));
}
var local = new BMap.LocalSearch(map, {
onSearchComplete: myFun
});
local.search(myValue);
map.addEventListener("click", function(e) {
map.clearOverlays();
map.addOverlay(new BMap.Marker(e.point))
var myGeo = new BMap.Geocoder();
th.lng = e.point.lng
th.lat = e.point.lat
myGeo.getLocation(e.point, function(result) {
th.address_detail = null
th.address_detail = result.address;
})
})
}
}
}
}
<style scoped lang="scss">
.baidumap {
width: 200px;
height: 200px;
margin-left: 150px;
margin-bottom: 20px;
}
.transit{
margin-left: 60px;
margin-bottom: 20px;
}
</style>