-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathindex.html
159 lines (139 loc) · 4.54 KB
/
index.html
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>ECharts China Map</title>
<style>
#china-map {
width: 1000px;
height: 800px;
position: absolute;
left: calc(50% - 500px);
top: 100px;
}
</style>
<script
type="text/javascript"
src="https://unpkg.com/echarts/dist/echarts.min.js"
></script>
<script
type="text/javascript"
src="https://unpkg.com/axios/dist/axios.min.js"
></script>
</head>
<body>
<button id="back" style="display: none">返回全国</button>
<div id="china-map"></div>
<script>
window.onload = function () {
const chart = echarts.init(document.querySelector("#china-map"));
const oBack = document.querySelector("#back");
// 地图缓存数据
let mapCaches = {};
// 点击进入下一级时,其父级的地图数据对象集合
let childParentMapObj;
function getAreaMap(areaCode, isFull) {
return axios
.get(
"http://42.192.232.58:3000/proxy/areas_v2/bound/" +
areaCode +
(isFull ? "_full" : "") +
".json?proxy_key=7f297416175d8b022098ded9548eb276"
)
.then(function (res) {
return Promise.resolve(res.data);
});
}
oBack.onclick = function () {
loadMap(childParentMapObj.map, childParentMapObj.name, {
code: childParentMapObj.code,
isFull: childParentMapObj.isFull,
});
if (childParentMapObj.map !== "china") {
childParentMapObj = childParentMapObj.parent;
oBack.innerHTML = "返回" + childParentMapObj.name;
} else {
childParentMapObj = null;
oBack.style.setProperty("display", "none");
}
};
loadMap("china", "中国", {
code: 100000,
isFull: true,
});
// 加载相应的地图
function loadMap(map, name, { code, isFull }) {
let promise = Promise.resolve();
if (!mapCaches[map]) {
promise = getAreaMap(code, isFull).then((mapData) => {
echarts.registerMap(map, mapData);
mapCaches[map] = mapData;
});
}
promise.then(() => {
const option = {
title: {
text: name,
left: "center",
},
series: [
{
name: name,
type: "map",
map,
roam: true, // 是否开启鼠标缩放和平移漫游
itemStyle: {
// 地图区域的多边形 图形样式
normal: {
// 是图形在默认状态下的样式
label: {
show: true, //是否显示标签
textStyle: {
color: "rgba(255,255,255,0)",
},
},
},
},
aspectScale: map === "china" ? 0.75 : 1,
top: "10%", //组件距离容器的距离
},
],
};
chart.clear();
chart.setOption(option);
chart.off("click");
chart.off("dblclick");
chart.on("click", function (param) {
if (param.name !== (childParentMapObj || {}).name) {
const mapData = mapCaches[map];
if (mapData) {
const mapInfo = mapData.features.find((m) => {
return m.properties.name === param.name;
});
const properties = mapInfo.properties;
loadMap(properties.adcode, properties.name, {
code: properties.adcode,
isFull: properties.level !== "district",
});
}
if (!childParentMapObj) {
oBack.style.setProperty("display", "block");
} else {
oBack.innerHTML = "返回" + name;
}
childParentMapObj = {
map,
name,
code,
isFull,
parent: childParentMapObj,
};
}
});
});
}
};
</script>
</body>
</html>