基于百度地图实现带箭头的轨迹,先上效果图
再接全部代码
<template>
<div class="container">
<baidu-map class="map" :scroll-wheel-zoom="true"
:center="center" :zoom="zoom" @ready="ready">
<new-polyline
v-if="points && points.length >= 2 && checkPoints({ points })"
:path="points"
:icons="icons"
stroke-color="#0091ea"
:stroke-opacity="0.8"
:stroke-weight="10"
></new-polyline>
</baidu-map>
</div>
</template>
<script>
import newPolyline from "./new_polyline";
export default {
components: {
newPolyline
},
data() {
return {
center: {
lng: 116.404,
lat: 39.915
},
zoom: 5,
points: [],
icons: []
};
},
methods: {
ready({ BMap, map }) {
this.points = this.getPointsSomehow();
console.log(this.points)
var sy = new BMap.Symbol(BMap_Symbol_SHAPE_FORWARD_OPEN_ARROW, {
scale: 0.5, // 图标缩放大小
strokeColor: "#fff", // 设置矢量图标的线填充颜色
strokeWeight: "3" // 设置线宽
});
var icons = new BMap.IconSequence(sy, "5%", "15%");
this.icons.push(icons)
},
getPointsSomehow() {
var arr = [
{ lng: 116.324356, lat: 39.980648 },
{ lng: 118.532031, lat: 32.010158 },
{ lng: 121.475599, lat: 31.380939 }
];
return arr;
},
// 查重 如果数组中只有一组有意义的坐标,绘制折线时可能会报错,因为绘制一条折线需要两组不同的坐标点
checkPoints({ points }) {
// 拿到第一组点
var originPoint = points[0];
// 将第一组点与后续点进行对比 如果坐标集合中只有一组实际坐标点则return false
// 只要坐标集合中有两组不同的实际坐标点,则return true
for (var i = 1; i < points.length; i++) {
if (
points[i].lat !== originPoint.lat ||
points[i].lng !== originPoint.lng
) {
return true;
}
}
return false;
}
}
};
</script>
<style>
.map {
width: 100%;
height: 100vh;
}
</style>
复制一个新的轨迹组件 new_polyline.vue
<script>
/**
* 注意此处三个引入路径
* 在源文件中使用的是相对路径
* 但是因为现在是自定义组件,所以要重新调整路径
*/
import commonMixin from "vue-baidu-map/components/base/mixins/common.js";
import bindEvents from "vue-baidu-map/components/base/bindEvent.js";
import { createPoint } from "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>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137327.html