现在挺多博主都做的挺完整的地图文档使用介绍等等,但有时候我们接触去选择自己做的效果时候还是会遇到很多不同的问题。
这里我把我自己个人在各个检索下的文章,做出自己想要的效果地图,基础地图功能+路线+运动轨迹的过程介绍一下对应组件使用与注意事项。
效果图
PC端,有切换地图类型、定位、缩放、标签、路线、轨迹等效果的百度地图嵌入。
第一步:先下载百度插件
npm install vue-baidu-map --save
第二步:获取百度地图平台的ak密钥
就是在百度地图自己去注册申请一个ak密钥。
第一步:点击此链接jspopularGL | 百度地图API SDK (baidu.com)
第一步:点击此链接
第三步:在main.js里面全局注册
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
ak: '这个地方填你的ak密钥' //这个地方是官方提供的ak密钥
})
完整代码:
<template>
<!-- 地图 -->
<div style="float:left;width:100%">
<el-card style="height: 60vh;margin-top:20px">
<!-- <el-input placeholder="请输入地址" v-model="input3" class="input-with-select">
<el-button slot="append" icon="el-icon-search" @click="inputfz">搜索</el-button>
</el-input> -->
<!--
scroll-wheel-zoom是是否可以缩放
@ready是图加载完后触发事件
center是位置定位
zoom是缩放大小限制
inertial-dragging是允许惯性拖拽
-->
<baidu-map class="bm-view" :zoom="15" :center="center" inertial-dragging @ready="mapReady"
:scroll-wheel-zoom="true">
<!-- 定位控件 anchor="BMAP_ANCHOR_BOTTOM_RIGHT"代表放在右下角 -->
<bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true"
:autoLocation="true"></bm-geolocation>
<!-- 地区检索 keyword:关键字搜索 @searchcomplete:检索完成后的回调函数 auto-viewport:检索结束后是否自动调整地图事业 -->
<!-- <bm-local-search :keyword="keyword" @searchcomplete="search" :auto-viewport="true"
class="search"></bm-local-search> -->
<!-- 地图类型切换 -->
<bm-map-type :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']"
anchor="BMAP_ANCHOR_TOP_LEFT"></bm-map-type>
<!-- 缩放控件 anchor代表控件停靠位置 anchor="BMAP_ANCHOR_TOP_RIGHT"代表放在右上角-->
<bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
<!-- 路书 -->
<!-- <bm-driving :start="start" :end="end" :panel="false" @stop="reset" :autoViewport="false"></bm-driving> -->
<bml-lushu :path="lushuPath" :icon="icon" :speed="speed" :content="powerSips" :rotation="true">
</bml-lushu>
<!-- 轨迹 -->
<new-polyline :path="polylinePath" :editing="false" :icons="icons" stroke-color="#8ba0fb"
:stroke-opacity="1" :stroke-weight="8" @lineupdate="updatePolylinePath"></new-polyline>
<!-- 起点终点 -->
<bm-marker :position="startPoint" :dragging="true" @click="infoWindowOpen">
<bm-label content="起点" :labelStyle="{ color: 'black', fontSize: '16px' }"
:offset="{ width: -35, height: 30 }" />
</bm-marker>
<bm-marker :position="endPoint" :dragging="true" @click="infoWindowOpen">
<bm-label content="终点" :labelStyle="{ color: 'black', fontSize: '16px' }"
:offset="{ width: -35, height: 30 }" />
</bm-marker>
</baidu-map>
</el-card>
</div>
</template>
<script>
import {
BaiduMap,
BmControl,
BmView,
BmAutoComplete,
BmLocalSearch,
BmMarker,
BmGeolocation,
BmlLushu
} from "vue-baidu-map";
import {
jgTransportDetails,
} from "@/api/regionalWaste/supervision";
import newPolyline from "./new_polyline.vue";
export default {
components: {
Dialog,
BaiduMap,
BmControl,
BmView,
BmAutoComplete,
BmLocalSearch,
BmMarker,
BmGeolocation,
newPolyline,
BmlLushu,
},
data() {
return {
//定位位置信息
center: {
lng: 116.404, lat: 39.915
},
// //检索关键字
// keyword: "",
// //输入框input值
// input3: "",
polylinePath: [
{ lng: 116.404, lat: 39.915 },
{ lng: 116.405, lat: 39.920 },
{ lng: 116.423493, lat: 39.907445 }
],
lushuPath: [],
startPoint: { lng: 116.404, lat: 39.915 },
endPoint: { lng: 116.423493, lat: 39.907445 },
icons: [],
icon: {
url: "https://s1.ax1x.com/2023/06/20/pC80IAJ.png",
size: {
width: 52,
height: 52,
},
powerSips: "医废运输", //轨迹名称
color: "", //轨迹颜色
start: "", //起点
end: "", //终点
speed: 20, //轨迹速度
powerShow: false, //轨迹显示隐
enableRotation: true,
BMaps: undefined
},
}
},
methods: {
getInfo() {
jgTransportDetails(this.Gjid).then(res => {
if (res.code === 200) {
this.form = res.data
let routeList = []
routeList = JSON.parse(res.data.routes).map(i => {
return {
lng: i.jindu,
lat: i.weidu,
}
});
this.polylinePath = routeList
// this.form.weightDifference = res.data.yfDetailList.weightDifference
this.guiji()
}
})
},
//输入框
// inputfz() {
// this.keyword = this.input3;
// },
//地图加载后的回调
mapReady({ BMap, map }) {
this.getInfo()
this.lushuPath = []
//保存this指向,因为在百度的回调中this不指向vue
const _this = this;
// 路书车辆旋转
this.polylinePath.forEach(item => {
this.lushuPath.push(
new BMap.Point(item.lng, item.lat)
)
})
// 路线箭头颜色
var sy = new BMap.Symbol(BMap_Symbol_SHAPE_FORWARD_OPEN_ARROW, {
scale: 1, // 图标缩放大小
strokeColor: "#fff", // 设置矢量图标的线填充颜色
strokeWeight: "2" // 设置线宽
});
var icons = new BMap.IconSequence(sy, "10", "30");
_this.icons.push(icons)
// 获取自动定位方法
// var geolocation = new BMap.Geolocation();
// // 获取自动定位获取的坐标信息
// geolocation.getCurrentPosition(
// function (r) {
// //可以conso.log看一下这个r,他里面包含了检索到的位置信息。下面就把两个维度信息赋值给center来定位
// _this.center = {
// lng: r.point.lng,
// lat: r.point.lat,
// };
// },
// //启用高精度
// { enableHighAccuracy: true }
// );
},
reset() {
this.play = false;
},
//轨迹
guiji() {
// this.center = this.lineList[0];
this.start = this.polylinePath[0];
this.end = this.polylinePath[this.polylinePath.length - 1];
this.play = true;
},
},
created() {
if (this.Gjid) {
this.getInfo()
}
}
}
</script>
<style>
/* 给个宽高 */
.bm-view {
height: 500px;
margin-top: 20px;
width: 100%;
}
.input-with-select {
width: 400px;
margin-bottom: 5px;
}
.search {
height: 300px;
overflow: auto;
}
</style>
以上我写的都是用到vue-baidu-map的文档里面的组件,大部分都是集中在这几个组件集里面,里面的属性都写的很详细,容易上手与优化
补充一个点,要是需要像我这样要绘制方向箭头折线,需要重新修改组件,建立新的,详细解析请参考以下这个博主的分享链接:http://t.csdn.cn/nHjjw
以下是我的箭头折线更改组件代码
<template>
</template>
<script>
/**
* 注意此处三个引入路径
* 在源文件中使用的是相对路径
* 但是因为现在是自定义组件,所以要重新调整路径
*/
import commonMixin from "../../../../node_modules/vue-baidu-map/components/base/mixins/common.js";
import bindEvents from "../../../../node_modules/vue-baidu-map/components/base/bindEvent.js";
import { createPoint } from "../../../../node_modules/vue-baidu-map/components/base/factory.js";
export default {
// 起一个新名字
name: "new-polyline",
render() {},
mixins: [commonMixin("overlay")],
props: {
path: {
type: Array
},
// 新声明一个icons
icons: {
type: Array
},
strokeColor: {
type: String
},
strokeWeight: {
type: Number
},
strokeOpacity: {
type: Number
},
strokeStyle: {
type: String
},
massClear: {
type: Boolean,
default: true
},
clicking: {
type: Boolean,
default: true
},
editing: {
type: Boolean,
default: false
}
},
watch: {
path: {
handler(val, oldVal) {
this.reload();
},
deep: true
},
strokeColor(val) {
this.originInstance.setStrokeColor(val);
},
strokeOpacity(val) {
this.originInstance.setStrokeOpacity(val);
},
strokeWeight(val) {
this.originInstance.setStrokeWeight(val);
},
strokeStyle(val) {
this.originInstance.setStrokeStyle(val);
},
editing(val) {
val
? this.originInstance.enableEditing()
: this.originInstance.disableEditing();
},
massClear(val) {
val
? this.originInstance.enableMassClear()
: this.originInstance.disableMassClear();
},
clicking(val) {
this.reload();
}
},
methods: {
load() {
const {
BMap,
map,
path,
// 参数解构 加入icons
icons,
strokeColor,
strokeWeight,
strokeOpacity,
strokeStyle,
editing,
massClear,
clicking
} = this;
const overlay = new BMap.Polyline(
path.map(item =>
createPoint(BMap, {
lng: parseFloat(item.lng),
lat: parseFloat(item.lat)
})
),
{
strokeColor,
strokeWeight,
strokeOpacity,
strokeStyle,
// 配置icons
icons,
enableEditing: editing,
enableMassClear: massClear,
enableClicking: clicking
}
);
this.originInstance = overlay;
map.addOverlay(overlay);
bindEvents.call(this, overlay);
}
}
};
</script>