forked from markusand/mapboxgl-legend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
223 lines (201 loc) · 6.56 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
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>MapboxGL LegendControl example</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v2.0.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.0.0/mapbox-gl.css" rel="stylesheet" />
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="module" lang="ts">
import { Map } from 'mapbox-gl';
import LegendControl from './src';
const map = new Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
zoom: 12,
center: [-122.447303, 37.753574],
accessToken: import.meta.env.VITE_MAPBOX_TOKEN,
});
const legend = new LegendControl({
collapsed: false,
toggler: true,
// layers: ['facilities', 'life-expectancy'],
layers: {
'life-expectancy': true,
'facilities': true,
'ethnicity': true,
'fire-events': true,
'patterns': true,
// 'fire-events': ['circle-radius'],
},
});
map.addControl(legend, 'bottom-left');
setTimeout(() => {
legend.addLayers(['muni-routes']);
setTimeout(() => legend.removeLayers(['muni-routes']), 5000);
}, 5000);
map.on('load', () => {
map.addSource('ethnicity', { type: 'vector', url: 'mapbox://examples.8fgz4egr' });
map.addSource('neighborhoods', {
type: 'geojson',
data: 'https://gist.githubusercontent.com/cdolek/d08cac2fa3f6338d84ea/raw/ebe3d2a4eda405775a860d251974e1f08cbe4f48/SanFrancisco.Neighborhoods.json',
});
map.addSource('fireevents', {
type: 'geojson',
data: 'https://data.sfgov.org/resource/wr8u-xric.geojson',
});
map.addSource('facilities', {
type: 'geojson',
data: 'https://data.sfgov.org/resource/nc68-ngbr.geojson',
});
map.addLayer({
id: 'ethnicity',
type: 'circle',
source: 'ethnicity',
'source-layer': 'sf2010',
paint: {
'circle-color': [
'match',
['get', 'ethnicity'],
'White', '#f1e8c8',
'Black', '#443722',
'Hispanic', '#aa9761',
'Asian', '#e8e59d',
'#ccc',
],
},
});
map.addLayer({
id: 'fire-events',
type: 'circle',
source: 'fireevents',
paint: {
'circle-color': [
'step',
['to-number', ['get', 'suppression_personnel']],
'#ccc',
5, '#d3f2a3',
10, '#4c9b82',
20, '#074050',
],
'circle-opacity': 0.5,
'circle-radius': [
'interpolate', ['linear'],
['to-number', ['get', 'suppression_personnel']],
1, 5,
10, 15,
25, 25,
50, 35,
],
},
metadata: {
name: 'Fire events',
unit: 'k',
},
});
const lifeExpectancy = [
['Tenderloin', 72.2], ['SoMa', 73.5], ['Rincon Hill', 81.6], ['South Beach', 82.5], ['Chinatown', 83.2], ['Nob Hill', 81.1], ['Mission', 82.8], ['Ingleside', 81.8], ['Taraval', 82.8], ['Castro', 83.1], ['Lower Pacific Heights', 82.7], ['Panhandle', 82.8], ['Financial District', 86], ['Inner Richmond', 83.9], ['Outer Richmond', 84.1], ['Sunset', 83.9], ['Marina', 84.3], ['Bayview', 77.4], ['Portola', 84.7], ['Twin Peaks', 84.5], ['Lake Merced', 83.5], ['North Beach', 83.5], ['Bayshore', 81.9],
].flatMap(([name, exp]) => [['==', ['get', 'neighborhood'], name], exp]);
map.addLayer({
id: 'life-expectancy',
type: 'fill',
source: 'neighborhoods',
paint: {
'fill-color': [
'interpolate', ['linear'],
['case', ...lifeExpectancy, 0],
70, '#a16928',
80, '#edeac2',
90, '#2887a1',
],
'fill-opacity': 0.4,
},
metadata: {
name: 'Life expectancy',
unit: 'y',
},
});
map.addLayer({
id: 'facilities',
source: 'facilities',
type: 'symbol',
layout: {
'icon-image': [
'match', ['get', 'department_name'],
'Recreation And Parks', 'park-15',
'Public Health', 'hospital-15',
'School District (Sfusd)', 'school-15',
'Public Library', 'library-15',
'Fire Department', 'fire-station-15',
'Police Department', 'police-15',
'Airport (Sfo)', 'airport-15',
'Municipal Transportation Agency', 'bus',
'Port', 'ferry',
'marker-11',
],
},
metadata: {
name: 'Facilities',
labels: {
'Recreation And Parks': 'Park & Recreation',
'School District (Sfusd)': 'School',
'Police Department': 'Police Station',
'Airport (Sfo)': 'Airport',
'Municipal Transportation Agency': 'Transportation',
},
},
});
map.addSource('muni-routes', {
type: 'geojson',
data: 'https://data.sfgov.org/api/geospatial/9exe-acju?method=export&format=GeoJSON',
});
map.addLayer({
id: 'muni-routes',
type: 'line',
source: 'muni-routes',
paint: {
'line-color': [
'match', ['get', 'service_ca'],
'Rapid Bus', '#f94144',
'Frequent', '#ffd166',
'Connector', '#43aa8b',
'Specialized', '#26547c',
'#aaa',
],
'line-width': 2,
},
});
map.loadImage(
'./assets/pattern.stripe.png',
(error, image) => {
if (error) throw error;
map.addImage('honeycomb', image);
map.addLayer({
id: 'patterns',
type: 'fill',
source: 'neighborhoods',
paint: {
'fill-pattern': 'honeycomb'
},
})
}
);
});
</script>
</body>
</html>