-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
226 lines (199 loc) · 9.61 KB
/
index.html
File metadata and controls
226 lines (199 loc) · 9.61 KB
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
220
221
222
223
224
225
226
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/de0f812dc0.js" crossorigin="anonymous"></script>
<!-- map -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin="" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.Default.css" />
<script src="https://unpkg.com/leaflet.markercluster@1.4.1/dist/leaflet.markercluster.js"></script>
<title>Hello, Map!</title>
<style>
.container-fluid>.row {
height: 100vh;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div class="container-fluid container">
<div class="row">
<div class="col-6">
<div id="map">
</div>
</div>
<div class="col-6">
<div class="row">
<!-- 下拉式選單 -->
<label for="country"></label>
<select class="form-select" id="country">
</select>
</div>
<div class="row">
<h2>
<!-- 每人用水量統計(月份/公升) -->
<strong></strong>
</h2>
<civ class="col-12">
<table class="table table-bordered">
<thead>
<tr>
<th>1月</th>
<th>2月</th>
<th>3月</th>
<th>4月</th>
<th>5月</th>
<th>6月</th>
<th>7月</th>
<th>8月</th>
<th>9月</th>
<th>10月</th>
<th>11月</th>
<th>12月</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</civ>
</div>
</div>
</div>
</div>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js"
integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"
integrity="sha512-bZS47S7sPOxkjU/4Bt0zrhEtWx0y0CRkhEp8IckzK+ltifIIE9EMIMTuT/mEzoIMewUINruDBIR/jJnbguonqQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
//初始化地圖
let map = L.map("map", {
center: [25.03416068163684, 121.56454962636319], //經緯度
zoom: 9, //地圖縮放
})
//設定圖資來源
var osmUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osm = new L.TileLayer(osmUrl, { minZoom: 8, maxZoom: 19 }); //縮放範圍
map.addLayer(osm); //把圖層加到map中
//抓資料
//設定讀取網路資源(Axios)
//台灣行政區域經緯度
let areaDataRequest = axios.get('https://raw.githubusercontent.com/ChouJustice/ChouJustice.github.io/main/Map/%E5%8F%B0%E7%81%A3%E8%A1%8C%E6%94%BF%E5%9C%B0%E5%8D%80.json');
//各行政區每人平均用水量
let waterDataRequest = axios.get('https://raw.githubusercontent.com/ChouJustice/ChouJustice.github.io/main/Map/%E5%8F%B0%E7%81%A3%E8%87%AA%E4%BE%86%E6%B0%B4%E7%94%A8%E9%87%8F.json');
//全域變數宣告
let areaData; //行政區
let waterData //水用量資料
let areaRes;
let waterRes;
let waterAreaData; //行政區 + 水用量
let markers = L.markerClusterGroup();
function setMarker() {
if (markers) markers.clearLayers();
console.log(waterAreaData)
Object.keys(waterAreaData).forEach(x => {
let data = waterAreaData[x];
let marker = L.marker([data[0].Lat, data[0].Lng]);
let avg = (data.map(x => parseInt(x.TheDailyDomesticConsumptionOfWaterPerPerson)).reduce((a, b) => a + b) / 12).toFixed(2);
marker.bindPopup(
`<h4>${data[0].County} ${data[0].Town}</h4>
<p>整年平均每人用水量:${avg}公升</p>`
)
marker.addEventListener("click", function () {
document.querySelector("strong").innerText = `${data[0].County} ${data[0].Town}`;
let tbody = document.querySelector("tbody");
tbody.innerHTML = "";
let waterTr = document.createElement("tr");
data.forEach(x => {
let td = document.createElement("td");
td.innerText = x.TheDailyDomesticConsumptionOfWaterPerPerson;
waterTr.appendChild(td);
});
let tagTr = document.createElement("tr");
data.forEach(x => {
let td = document.createElement("td");
let icon = document.createElement("i");
icon.classList.add("fas", "fa-circle");
icon.style.color = parseFloat(x.TheDailyDomesticConsumptionOfWaterPerPerson) > avg ? "red" : "greenyellow";
td.appendChild(icon);
tagTr.appendChild(td);
})
tbody.appendChild(waterTr);
tbody.appendChild(tagTr);
});
markers.addLayer(marker);
map.addLayer(marker);
})
}
window.onload = function () {
Promise.all([areaDataRequest, waterDataRequest])
.then(response => {
[areaRes, waterRes] = response;
console.log(areaRes);
console.log(waterRes.data["TaiwanWaterExchangingData"]);
areaData = areaRes.data;
// console.log
waterData = waterRes.data.TaiwanWaterExchangingData.StatisticofWaterResourcesClass.StatisticofWaterUsageClass.TheConsumptionOfWater.filter(x => x.Year == "109");
console.log(areaData);
console.log(waterData);
waterAreaData = waterData.map(x => {
let area = areaData.find(y => y.City == x.County && y.District == x.Town);
return {
CountryTown: x.County + x.Town,
County: x.County,
Month: x.Month,
TheDailyDomesticConsumptionOfWaterPerPerson: x.TheDailyDomesticConsumptionOfWaterPerPerson,
Town: x.Town,
Year: x.Year,
Lat: area != undefined ? area.Lat : undefined,
Lng: area != undefined ? area.Lng : undefined
}
}).filter(x => x.Lat != undefined).groupBy("CountryTown");
console.log(waterAreaData)
setMarker();
//初始縣市下拉式選單
let countrySelect = document.querySelector("#country");
["請選擇"].concat([...new Set(areaData.map(x => x.City))]).forEach(city => {
let option = document.createElement("option");
option.innerText = city;
option.value = city == "請選擇" ? "" : city;
countrySelect.append(option);
})
})
}
document.querySelector("#country").addEventListener("change", function () {
console.log(this.value);
if (this.value != "") {
//改變地圖的焦點,把焦點移到該縣市
let country = areaData.find(x => x.City == this.value);
map.setView([country.Lat, country.Lng], 11);
}
});
Array.prototype.groupBy = function (prop) {
return this.reduce(function (groups, item) {
const val = item[prop];
groups[val] = groups[val] || [];
groups[val].push(item);
return groups;
}, {});
}
</script>
</body>
</html>