-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
214 lines (175 loc) · 5.9 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
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
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="2020 Taiwan population density">
<title>2020 Taiwan population density</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<!-- Make sure you put this AFTER Leaflet's CSS -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<style>
body {
padding: 0px;
margin: 0px;
}
html,
body,
#mapid {
position: absolute;
top:0;
bottom:0;
right:0;
left:0;
}
.info {
padding: 6px 8px;
font: 12px/15px Arial, Helvetica, sans-serif;
background: white;
background: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
}
.info h4 {
margin: 0 0 5px;
color: #777;
}
.legend {
line-height: 18px;
color: #555;
}
.legend i {
width: 18px;
height: 18px;
float: left;
margin-right: 5px;
opacity: 0.7;
}
#map-title {
position: absolute;
margin-top: 8px;
margin-left: 45px;
float: left;
background: white;
border: 2px solid rgba(0,0,0,0.2);
padding: 6px 8px;
font-family: Helvetica;
font-weight: bold;
font-size: 15px;
z-index: 800;
}
</style>
</head>
<body>
<div id="map-title">2020年臺灣<br>人口密度(縣市)</div>
<div id="mapid"></div>
<script type="text/javascript" src="taiwan_json.js"></script> <!--add geoJson -->
<script>
//加入底圖
var mymap = L.map('mapid').setView([23.5, 121.5], 7);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> , <a href="https://www.mapbox.com/">Mapbox</a> '
,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1
}).addTo(mymap);
// add geoJson
L.geoJson(TAIWAN).addTo(mymap);
//add info
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
this.update();
return this._div;
};
// method that we will use to update the control based on feature properties passed
info.update = function (props) {
this._div.innerHTML = '<h4>臺灣人口密度</h4>' + (props ?
'<b>' + props.COUNTYNAME + '</b><br />' + props.POP_DEN + ' 人 / 平方公里'
: '移動滑鼠至各縣市');
};
info.addTo(mymap);
// hypsometric map
function getColor(d) {
return d > 9000 ? '#7f2704' :
d > 7000 ? '#a63603' :
d > 5000 ? '#d94801' :
d > 3000 ? '#f16913' :
d > 1000 ? '#fd8d3c' :
d > 500 ? '#fdae6b' :
d > 200 ? '#fdd0a2' :
d > 100 ? '#fee6ce' :
'#fff5eb';
}
// geoJson color
function style(feature) {
return {
fillColor: getColor(feature.properties.POP_DEN),
weight: 2,
opacity: 1,
color: 'white',
dashArray: '1',
fillOpacity: 0.7
};
}
//hover highlight
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 5,
color: 'blue',
dashArray: '',
fillOpacity: 0.7
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront();
}
info.update(layer.feature.properties);
}
var geojson;
//reset highlight
function resetHighlight(e) {
geojson.resetStyle(e.target);
info.update();
}
// zoom to when click county
function zoomToFeature(e) {
mymap.fitBounds(e.target.getBounds());
}
// function:mouse and click
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature
});
}
geojson = L.geoJson(TAIWAN, {
style: style,
onEachFeature: onEachFeature
}).addTo(mymap);
// add legend
var legend = L.control({position: 'bottomright'});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 100, 200, 500, 1000, 3000, 5000,7000, 9000],
labels = [];
// loop through our density intervals and generate a label with a colored square for each interval
for (var i = 0; i < grades.length; i++) {
div.innerHTML +=
'<i style="background:' + getColor(grades[i] + 1) + '"></i> ' +
grades[i] + (grades[i + 1] ? '–' + grades[i + 1] + '<br>' : '+');
}
return div;
};
legend.addTo(mymap);
mymap.attributionControl.addAttribution('Population data © <a href="https://www.ris.gov.tw/app/portal/346">中華民國內政部戶政司</a>');
</script>
</body>
</html>