### 确保地图加载完成并在mounted钩子中绘制绿色丝滑箭头路线
在前端框架(如Vue2)中实现地图加载完成后绘制绿色丝滑箭头路线,并标记起点和终点,同时解决`Uncaught TypeError: Cannot read properties of null (reading 'style')`错误,需要遵循以下方法。
#### 1. 地图初始化与加载完成
确保地图在`mounted`生命周期内加载完成。通过监听地图的`tilesloaded`事件[^1],可以确认地图完全加载后再进行其他操作。
```javascript
methods: {
initMap() {
const center = new qq.maps.LatLng(39.9042, 116.4074); // 设置地图中心点
this.map = new qq.maps.Map(document.getElementById("container"), {
center: center,
zoom: 12,
});
// 确保地图加载完成
qq.maps.event.addListenerOnce(this.map, "tilesloaded", () => {
this.drawTrajectory();
this.addMarkers();
});
},
},
```
#### 2. 标记起点和终点
通过`Marker`对象分别在起点和终点添加标记。设置不同的图标样式以区分起点和终点。
```javascript
methods: {
addMarkers() {
const startIcon = "https://example.com/start-icon.png";
const endIcon = "https://example.com/end-icon.png";
const startPoint = new qq.maps.LatLng(
this.trajectory[0].lat,
this.trajectory[0].lng
);
const endPoint = new qq.maps.LatLng(
this.trajectory[this.trajectory.length - 1].lat,
this.trajectory[this.trajectory.length - 1].lng
);
new qq.maps.Marker({
position: startPoint,
map: this.map,
icon: startIcon,
title: "起点",
});
new qq.maps.Marker({
position: endPoint,
map: this.map,
icon: endIcon,
title: "终点",
});
},
},
```
#### 3. 绘制带箭头的绿色轨迹
通过`Polyline`对象绘制轨迹,并使用自定义样式实现绿色轨迹和箭头效果。腾讯地图支持通过SVG路径定义箭头样式[^2]。
```javascript
methods: {
drawTrajectory() {
if (this.polyline) {
this.polyline.setMap(null);
}
const polylineOptions = {
path: this.trajectory.map((point) =>
new qq.maps.LatLng(point.lat, point.lng)
),
strokeColor: "#3CB371", // 绿色
strokeWeight: 5,
icons: [
{
icon: {
path: "M 0,-1 0,1 L 3,0 Z", // 自定义箭头SVG路径
fillColor: "#3CB371",
fillOpacity: 1,
scale: 2,
anchor: new qq.maps.Point(15, 15),
},
repeat: "50px", // 每隔50像素重复一次箭头
},
],
map: this.map,
};
this.polyline = new qq.maps.Polyline(polylineOptions);
},
},
```
#### 4. 解决TypeError错误
为避免`Uncaught TypeError: Cannot read properties of null (reading 'style')`错误,需在触发相关事件前进行判断处理。例如,在重置页面大小时触发事件时,应检查元素是否存在。
```javascript
mounted() {
window.addEventListener("resize", () => {
if (document.getElementById("container")) {
this.initMap(); // 确保地图容器存在时重新初始化地图
}
});
},
```
#### 完整代码示例
将上述功能整合到一个Vue组件中:
```javascript
<template>
<div>
<div id="container" style="width: 100%; height: 500px;"></div>
</div>
</template>
<script>
export default {
data() {
return {
map: null,
polyline: null,
trajectory: [
{ lat: 39.9042, lng: 116.4074 },
{ lat: 39.9142, lng: 116.4174 },
{ lat: 39.9242, lng: 116.4274 },
// 添加更多轨迹点...
],
};
},
mounted() {
this.initMap();
window.addEventListener("resize", () => {
if (document.getElementById("container")) {
this.initMap(); // 确保地图容器存在时重新初始化地图
}
});
},
methods: {
initMap() {
const center = new qq.maps.LatLng(39.9042, 116.4074);
this.map = new qq.maps.Map(document.getElementById("container"), {
center: center,
zoom: 12,
});
qq.maps.event.addListenerOnce(this.map, "tilesloaded", () => {
this.drawTrajectory();
this.addMarkers();
});
},
drawTrajectory() {
if (this.polyline) {
this.polyline.setMap(null);
}
const polylineOptions = {
path: this.trajectory.map((point) =>
new qq.maps.LatLng(point.lat, point.lng)
),
strokeColor: "#3CB371",
strokeWeight: 5,
icons: [
{
icon: {
path: "M 0,-1 0,1 L 3,0 Z",
fillColor: "#3CB371",
fillOpacity: 1,
scale: 2,
anchor: new qq.maps.Point(15, 15),
},
repeat: "50px",
},
],
map: this.map,
};
this.polyline = new qq.maps.Polyline(polylineOptions);
},
addMarkers() {
const startIcon = "https://example.com/start-icon.png";
const endIcon = "https://example.com/end-icon.png";
const startPoint = new qq.maps.LatLng(
this.trajectory[0].lat,
this.trajectory[0].lng
);
const endPoint = new qq.maps.LatLng(
this.trajectory[this.trajectory.length - 1].lat,
this.trajectory[this.trajectory.length - 1].lng
);
new qq.maps.Marker({
position: startPoint,
map: this.map,
icon: startIcon,
title: "起点",
});
new qq.maps.Marker({
position: endPoint,
map: this.map,
icon: endIcon,
title: "终点",
});
},
},
};
</script>
```
### 注意事项
- 确保腾讯地图API的Key已正确配置[^1]。
- 轨迹数据需提前准备,可从后端接口获取或手动录入。
- 箭头样式可通过调整SVG路径实现不同效果。