-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathindex_v1.html
219 lines (201 loc) · 5.29 KB
/
index_v1.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!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="./js/map/china.js"></script>
</head>
<body>
<button id="back" style="display: none">返回全国</button>
<div id="china-map"></div>
<script>
var myChart = echarts.init(document.getElementById("china-map"));
var oBack = document.getElementById("back");
var catches = {};
var provinces = [
"shanghai",
"hebei",
"shanxi",
"neimenggu",
"liaoning",
"jilin",
"heilongjiang",
"jiangsu",
"zhejiang",
"anhui",
"fujian",
"jiangxi",
"shandong",
"henan",
"hubei",
"hunan",
"guangdong",
"guangxi",
"hainan",
"sichuan",
"guizhou",
"yunnan",
"xizang",
"shanxi1",
"gansu",
"qinghai",
"ningxia",
"xinjiang",
"beijing",
"tianjin",
"chongqing",
"xianggang",
"aomen",
];
var provincesText = [
"上海",
"河北",
"山西",
"内蒙古",
"辽宁",
"吉林",
"黑龙江",
"江苏",
"浙江",
"安徽",
"福建",
"江西",
"山东",
"河南",
"湖北",
"湖南",
"广东",
"广西",
"海南",
"四川",
"贵州",
"云南",
"西藏",
"陕西",
"甘肃",
"青海",
"宁夏",
"新疆",
"北京",
"天津",
"重庆",
"香港",
"澳门",
];
oBack.onclick = function () {
loadMap("china", "中国");
oBack.style.setProperty('display', 'none');
};
loadMap("china", "中国");
// 加载地图
function loadMap(mapType, name) {
var option = {
title: {
text: name || mapType,
left: "center",
},
series: [
{
name: name || mapType,
type: "map",
mapType,
roam: false, //是否开启鼠标缩放和平移漫游
itemStyle: {
//地图区域的多边形 图形样式
normal: {
//是图形在默认状态下的样式
label: {
show: true, //是否显示标签
textStyle: {
color: "rgba(255,255,255,0)",
},
},
},
},
aspectScale: mapType === "china" ? 0.75 : 1,
top: "10%", //组件距离容器的距离
},
],
};
// 清理画布
myChart.clear();
myChart.setOption(option);
myChart.off("click");
if (mapType === "china") {
// 全国时,添加click 进入省级
myChart.on("click", function (param) {
//遍历取到provincesText 中的下标 去拿到对应的省js
for (var i = 0; i < provincesText.length; i++) {
if (param.name === provincesText[i]) {
//显示对应省份的方法
showProvince(provinces[i], provincesText[i]);
break;
}
}
oBack.style.setProperty('display', 'block');
});
} else {
// 省份,添加双击 回退到全国
myChart.on("dblclick", function () {
loadMap("china", "中国");
});
}
}
// 展示对应的省
function showProvince(mapType, name) {
//这写省份的js都是通过在线构建工具生成的,保存在本地,需要时加载使用即可,最好不要一开始全部直接引入。
if (catches[name]) {
loadMap(name);
} else {
catches[mapType] = true;
loadBdScript(
"$" + mapType + "JS",
"./js/map/province/" + mapType + ".js",
function () {
loadMap(name);
}
);
}
}
// 加载对应的JS
function loadBdScript(scriptId, url, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) {
//IE
script.onreadystatechange = function () {
if (
script.readyState === "loaded" ||
script.readyState === "complete"
) {
script.onreadystatechange = null;
callback();
}
};
} else {
// Others
script.onload = function () {
callback();
};
}
script.src = url;
script.id = scriptId;
document.getElementsByTagName("head")[0].appendChild(script);
}
</script>
</body>
</html>