-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
173 lines (157 loc) · 5.15 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Create a time slider</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.js"></script>
<script src="mapbox-token-setup.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<div id="map"></div>
<div class="map-overlay top">
<div class="map-overlay-inner">
<h2>Significant earthquakes in 2015</h2>
<label id="month"></label>
<input id="slider" type="range" min="0" max="11" step="1" value="0">
<button type="button" id="play" class="play-button"></button>
</div>
<div class="map-overlay-inner">
<div id="legend" class="legend">
<div class="bar"></div>
<div>Magnitude (m)</div>
</div>
</div>
</div>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
mapboxgl.accessToken = myToken;
const map = new mapboxgl.Map({
container: 'map',
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/light-v10',
center: [31.4606, 20.7927],
zoom: 0.5
});
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
function filterBy(month) {
const filters = ['==', 'month', month];
map.setFilter('earthquake-circles', filters);
map.setFilter('earthquake-labels', filters);
// Set the label to the month
document.getElementById('month').textContent = months[month];
}
map.on('load', () => {
// Data courtesy of http://earthquake.usgs.gov/
// Query for significant earthquakes in 2015 URL request looked like this:
// http://earthquake.usgs.gov/fdsnws/event/1/query
// ?format=geojson
// &starttime=2015-01-01
// &endtime=2015-12-31
// &minmagnitude=6'
//
// Here we're using d3 to help us make the ajax request but you can use
// Any request method (library or otherwise) you wish.
d3.json(
'https://docs.mapbox.com/mapbox-gl-js/assets/significant-earthquakes-2015.geojson',
jsonCallback
);
});
function jsonCallback(err, data) {
if (err) {
throw err;
}
// Create a month property value based on time
// used to filter against.
data.features = data.features.map((d) => {
d.properties.month = new Date(d.properties.time).getMonth();
return d;
});
map.addSource('earthquakes', {
'type': 'geojson',
data: data
});
map.addLayer({
'id': 'earthquake-circles',
'type': 'circle',
'source': 'earthquakes',
'paint': {
'circle-color': [
'interpolate',
['linear'],
['get', 'mag'],
6,
'#FCA107',
8,
'#7F3121'
],
'circle-opacity': 0.75,
'circle-radius': [
'interpolate',
['linear'],
['get', 'mag'],
6,
20,
8,
40
]
}
});
map.addLayer({
'id': 'earthquake-labels',
'type': 'symbol',
'source': 'earthquakes',
'layout': {
'text-field': ['concat', ['to-string', ['get', 'mag']], 'm'],
'text-font': ['Open Sans Bold', 'Arial Unicode MS Bold'],
'text-size': 12
},
'paint': {
'text-color': 'rgba(0,0,0,0.5)'
}
});
// Set filter to first month of the year
// 0 = January
filterBy(0);
document.getElementById('slider').addEventListener('input', (e) => {
const month = parseInt(e.target.value, 10);
filterBy(month);
});
}
// Code copied from https://stackoverflow.com/a/34935566
let myTimer;
let play_state = false;
d3.select("#play").on("click", function() {
if (!play_state) {
clearInterval (myTimer);
myTimer = setInterval (function() {
let b = d3.select("#slider");
let t = (+b.property("value") + 1) % (+b.property("max") + 1);
if (t == 0) { t = +b.property("min"); }
b.property("value", t);
document.getElementById('slider').dispatchEvent(new Event('input'));
}, 500);
} else {
clearInterval (myTimer);
}
play_state = !play_state;
document.getElementById('play').classList.toggle("paused");
});
</script>
</body>
</html>