百度地图Polyline不显示的另类解决方案

木头的喵喵拖孩

项目需要使用百度地图,但是项目是不连接互联网的,所以无法通过正常方式引用百度地图 API,只用通过代理使用百度地图 API,类似于下面的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>

<!-- 通过代理地址“192.168.1.1”调用百度地图API -->
<script
type="text/javascript"
src="http://192.168.1.1/api.map.baidu.com/api?v=3.0&ak=您的密钥"
></script>
</head>

<body></body>
</html>

此种代理方式会导致百度地图的折线Polyline无法正常使用(原因未知),所以需要另辟蹊径解决。
我的方法就是根据已知的经纬度点数组,通过SVG把折线画出来,然后覆盖到百度地图上,替代无法使用的Polyline
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>

<style>
body {
margin: 0;
padding: 0;
width: 100vw;
height: 100vh;
}

#container {
position: relative;
width: 100%;
height: 100%;
}

#map {
width: 100%;
height: 100%;
}

#mySvg {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
</style>

<!-- 调用百度地图API -->
<script
type="text/javascript"
src="https://api.map.baidu.com/api?v=3.0&ak=您的ak密钥"
></script>
</head>

<body>
<div id="container">
<div id="map"></div>
<svg id="mySvg" xmlns="http://www.w3.org/2000/svg"></svg>
</div>

<script>
// 经纬度点数组
const points = [
{ lng: 116.404, lat: 39.915 },
{ lng: 116.405, lat: 39.92 },
{ lng: 116.423493, lat: 39.907445 },
];

// 格式化百度地图经纬度
const formatPoint = (point) => {
if (point instanceof Array) {
return new BMap.Point(parseFloat(point[0]), parseFloat(point[1]));
} else if (point instanceof Object) {
return new BMap.Point(parseFloat(point.lng), parseFloat(point.lat));
}
};

const $map = document.getElementById("map");
const $mySvg = document.getElementById("mySvg");

// 实例化地图
const map = new BMap.Map($map);
map.enableScrollWheelZoom();
map.disableDoubleClickZoom();
map.centerAndZoom(formatPoint(points[0]), 15);

// 绘制自定义折线
const addCusPolyline = () => {
// 核心代码,转化经纬度点为DOM的偏移位置
let pixels = points.map((point) =>
map.pointToPixel(formatPoint(point))
);

let d = "";
for (let i = 0; i < pixels.length; i++) {
let pixel = pixels[i];
if (i === 0) {
d += `M ${pixel.x}, ${pixel.y} `;
} else {
d += `L ${pixel.x}, ${pixel.y} `;
}
}
let pathStr = `<path d="${d}" style="fill-opacity: 0; stroke: red; stroke-width: 2;"></path> `;
$mySvg.setAttribute(
"viewBox",
`0 0 ${$mySvg.clientWidth} ${$mySvg.clientHeight}`
);
$mySvg.innerHTML = pathStr;
};
addCusPolyline();

// 清空自定义折线
const removePolyline = () => {
$mySvg.innerHTML = "";
};

// 地图事件处理,为了更好的展示自定义折线图
map.removeEventListener("zoomstart", removePolyline);
map.removeEventListener("dragstart", removePolyline);
map.removeEventListener("tilesloaded", addCusPolyline);
map.addEventListener("zoomstart", removePolyline);
map.addEventListener("dragstart", removePolyline);
map.addEventListener("tilesloaded", addCusPolyline);
</script>
</body>
</html>
  • 标题: 百度地图Polyline不显示的另类解决方案
  • 作者: 木头的喵喵拖孩
  • 创建于: 2023-12-26 14:28:44
  • 更新于: 2024-05-21 10:56:15
  • 链接: https://blog.xx-xx.top/2023/12/26/百度地图Polyline不显示的另类解决方案/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
百度地图Polyline不显示的另类解决方案