- 先去百度地图开放平台拿ak
- 安装
vue-baidu-map
- 参考下面的例子的代码(该例子调成移动端浏览即可精确定位)
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
ak: 'O4Vh4wE3dqCWhut4ncQMYTZeINrXX2SB'
})
<template>
<div class="hello">
<baidu-map id="map" class="map"
:center="center"
:zoom="zoom"
:MapType="'BMAP_NORMAL_MAP'"
@ready="handler">
</baidu-map>
<div class="btn-group" style="display:none">
<button class="btn" @click="zoomOut">+</button>
<button class="btn" @click="zoomIn">-</button>
</div>
</div>
</template>
<script>
export default {
name: 'Map',
data() {
return {
center: { lng: 0, lat: 0, },
BMap: {},
map: {},
zoom: 12,
point: {},
BpPoint: {},
marker: false,
};
},
mounted() {
},
props: {
routeLine: {
type: Object,
default: () => ({
location: '',
myLocal: '',
}),
},
},
methods: {
handler({ BMap, map, }) {
this.BMap = BMap;
this.map = map;
this.getCurrentLocation();
},
getCurrentLocation() {
const geolocation = new this.BMap.Geolocation();
geolocation.getCurrentPosition((res) => {
if (res) {
const { point, } = res;
this.point = res.point;
window.point = res.point;
this.BpPoint = new this.BMap.Point(point.lng, point.lat);
console.log('point', point);
this.getLocationByPoint(res.point);
}
});
},
getLabel(res) {
return new this.BMap.Label(res.address, { offset: new this.BMap.Size(20, -10), });
},
getLocationByPoint() {
const geoc = new this.BMap.Geocoder();
geoc.getLocation(this.BpPoint, (res) => {
if (!this.marker) {
this.marker = new this.BMap.Marker(this.BpPoint);
this.map.addOverlay(this.marker);
this.adjustMarker(this.getLabel(res));
} else {
this.adjustMarker(this.getLabel(res));
}
});
},
adjustMarker(label) {
this.marker.setLabel(label);
this.marker.setAnimation(window.BMAP_ANIMATION_BOUNCE);
this.map.centerAndZoom(this.BpPoint, this.zoom);
this.showRoute();
this.$emit('ready', this.point);
},
zoomIn() {
this.map.zoomIn();
if (this.zoom < 19) {
this.zoom++;
} else {
alert('地图级别在4-19之间');
}
},
zoomOut() {
this.map.zoomOut();
if (this.zoom > 4) {
this.zoom--;
} else {
alert('地图级别在4-19之间');
}
},
goToCurrentLocation() {
this.getLocationByPoint();
},
showRoute() {
const driving = new this.BMap.DrivingRoute(this.map, {
renderOptions: {
map: this.map,
autoViewport: true,
},
onSearchComplete(results) {
console.log('results', results);
if (driving.getStatus() == window.BMAP_STATUS_SUCCESS) {
const plan = results.getPlan(0);
const route = plan.getRoute(0);
for (let i = 0; i < route.getNumSteps(); i++) {
const step = route.getStep(i);
console.log(step);
}
}
},
});
console.log(123);
const localArr = this.routeLine.location.split(',');
const myLocalArr = this.routeLine.myLocal.split(',');
console.log(Number(localArr[0]), Number(localArr[1]));
console.log(Number(myLocalArr[0]), Number(localArr[1]));
console.log('localArr', localArr);
console.log('myLocalArr', myLocalArr);
const start = new this.BMap.Point(Number(localArr[1]), Number(localArr[0]));
const end = new this.BMap.Point(Number(myLocalArr[1]), Number(myLocalArr[0]));
driving.search(start, end);
},
},
};
</script>
<style lang="less" scoped>
@import url("./main.less");
.test {
.calc-vw(222);
height: 10rem;
background-color: red;
}
.map {
width: 100vw;
.calc-vh(200);
}
.btn-group {
position: absolute;
left: 0;
top: 0;
display: flex;
justify-content: center;
.calc-vw(20);
flex-wrap: wrap;
.btn {
display: block;
margin-bottom: 20px;
}
.locate {
.calc-font-size(25);
color: red;
}
}
</style>
@sceenWidth:375;
.calc-vw(@width) {
width: @width/@sceenWidth*100vw
}
.calc-vh(@width) {
height: @width/@sceenWidth*100vw
}
.calc-font-size(@font-size) {
font-size: @font-size/@sceenWidth*100vw
}
.calc-left(@left) {
left: @left/@sceenWidth*100vw
}
.calc-margin-left(@margin-left) {
margin-left: @margin-left/@sceenWidth*100vw
}
.calc-margin-right(@margin-right) {
margin-right: @margin-right/@sceenWidth*100vw
}
.calc-margin-bottom(@margin-bottom) {
margin-bottom: @margin-bottom/@sceenWidth*100vw
}
.calc-margin-top(@margin-top) {
margin-top: @margin-top/@sceenWidth*100vw
}
.calc-padding-left(@padding-left) {
padding-left: @padding-left/@sceenWidth*100vw
}
.calc-padding-right(@padding-right) {
padding-right: @padding-right/@sceenWidth*100vw
}
.calc-padding-bottom(@padding-bottom) {
padding-bottom: @padding-bottom/@sceenWidth*100vw
}
.calc-padding-top(@padding-top) {
padding-top: @padding-top/@sceenWidth*100vw
}
.calc-top(@top) {
top: -(@top/@sceenWidth*100vw)
}
img {
max-width: 100%;
height: auto!important;
}
@mainColor: #44D7B6;