vue百度地图的使用
- 第一步:安装插件
- 第二步:main.js中引用
- 第三步:页面中使用
第一步:安装插件
npm install vue-baidu-map --save
第二步:main.js中引用
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
ak: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
})
第三步:页面中使用
<template>
<div>
<el-form ref="Froms" :model="formData" :rules="rules" label-width="120px">
<el-form-item label="详细地址:" prop="shopAddrExt">
<el-input v-model="formData.shopAddrExt" placeholder="请输入详细地址" size="small" @input="inputShopAddr" />
<div class="map">
<baidu-map :center="center" :zoom="zoom" :scroll-wheel-zoom="true" @ready="initMap" @click="getLocation">
<!-- 缩放 -->
<bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT" />
</baidu-map>
</div>
</el-form-item>
<el-form-item label="经营地区:" prop="businessArea">
<!-- 要安装v-region -->
<v-region-group
:town="false"
v-model="regionArea"
@change="regionChange"
>
</v-region-group>
</el-form>
</div>
</template>
<script>
export default {
data () {
return {
formData: {shopAddrExt: '', businessArea: ''},
center: {lng: 0, lat: 0},
zoom: 10,
BMap: null,
map: null,
rules:{},
regionArea: {}
}
},
methods: {
initMap ({BMap, map}) {
console.log(BMap, map)
this.zoom = 15;
this.BMap = BMap;
this.map = map;
this.geoCoder = new BMap.Geocoder();
let geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition((e)=>{
console.log(e);
this.center.lng = e.longitude;
this.center.lat = e.latitude;
this.setCenterZoom(e);
this.setIcon(e);
}
this.map.addEventListener('zoomend', (e)=> {
this.map.centerAndZoom(
new BMap.Point(this.center.lng, this.center.lat),
this.map.getZoom()
);
});
},
setCenterZoom(e){
this.center.lng = e.longitude;
this.center.lat = e.latitude;
this.centerPoint = new BMap.Point(e.longitude, e.latitude);
this.map.centerAndZoom(this.centerPoint, 14);
},
setIcon(latLng){
const icon = new BMap.Icon(
require('../../../../../static/icon/position4.png'),
new BMap.Size(32, 32),
)
const point = new BMap.Point(latLng.longitude, latLng.latitude);
const marker = new BMap.Marker(point, { icon: icon });
this.map.clearOverlays();
this.map.addOverlay(marker);
marker.addEventListener('click', (e) => {
console.log(e)
})
},
getLocation(e){
let latLng = {
longitude: e.point.lng,
latitude: e.point.lat
}
this.setCenterZoom(latLng);
this.setIcon(latLng);
this.geoCoder.getLocation(e.point, (rs) => {
let adr = rs.addressComponents;
let address = adr.province + adr.city + adr.district + adr.street + adr.streetNumber;
this.formData.shopAddrExt = address;
})
},
inputShopAddr(inputValue){
this.geoCoder.getPoint(inputValue, (point) => {
let latLng = {
longitude: point.lng,
latitude: point.lat
}
this.setCenterZoom(latLng);
this.setIcon(latLng);
})
},
regionChange (data) {
console.log(data)
let province = data.province ? data.province.value : '';
let city = data.city ? data.city.value : '';
let area = data.area ? data.area.value : '';
this.formData.businessArea = province + city + area;
this.formData.shopProvinceId = data.province ? data.province.key : '';
this.formData.shopCityId = data.city ? data.city.key : '';
this.formData.shopCountryId = data.area ? data.area.key : '';
this.inputShopAddr(this.formData.businessArea);
}
}
</script>
<style>
.map{
width: 430px;
height: 280px;
}
.BMap_cpyCtrl,
.BMap_noprint {
display: none;
}
.BMap_cpyCtrl,
.anchorBL {
inset: auto auto 0px 1px !important;
}
</style>