-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
207 lines (189 loc) · 5.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Display a map</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
#legend {
position: absolute;
bottom: 75px;
right: 75px;
background: white;
border-radius: 5px;
border: 1px solid black;
padding: 8px 12px;
}
.layer {
margin: 12px 5px;
}
.color {
width: 20px;
height: 20px;
background: gray;
display: inline-block;
margin-right: 5px;
vertical-align: middle;
}
.state-house .color {
background: #00bbff;
}
.state-senate .color {
background: #ff004c;
}
.us-house .color {
background: #ff9500;
}
.us-senate .color {
background: #70d644;
}
.disabled {
opacity: 0.3;
}
.layer:hover {
cursor: pointer;
opacity: 0.8;
}
#debug {
position: absolute;
left: 20px;
bottom: 20px;
font-size: 10px
}
</style>
</head>
<body>
<div id="map"></div>
<div id="legend">
<div class="layer state-house" data-layer="state-house">
<span class="color"></span>state house
</div>
<div class="layer state-senate disabled" data-layer="state-senate">
<span class="color"></span>state senate
</div>
<div class="layer us-house disabled" data-layer="us-house">
<span class="color"></span>us house
</div>
<div class="layer us-senate disabled" data-layer="us-senate">
<span class="color"></span>us senate
</div>
</div>
<div id="debug">
</div>
<script>
var layerStyles = {
'state-house': {
"id": "state-house-fill",
"source": "us-constituencies",
"source-layer": "state-house",
"type": "fill",
"paint": {
"fill-color": "#00bbff",
"fill-opacity": 0.4
}
},
'state-senate': {
"id": "state-senate-fill",
"source": "us-constituencies",
"source-layer": "state-senate",
"type": "fill",
"paint": {
"fill-color": "#ff004c",
"fill-opacity": 0.4
}
},
'us-house': {
"id": "us-house-fill",
"source": "us-constituencies",
"source-layer": "us-house",
"type": "fill",
"paint": {
"fill-color": "#ff9500",
"fill-opacity": 0.4
}
},
'us-senate': {
"id": "us-senate-fill",
"source": "us-constituencies",
"source-layer": "us-senate",
"type": "fill",
"paint": {
"fill-color": "#70d644",
"fill-opacity": 0.4
}
}
};
var style = {
"version": 8,
"name": "Custom Vector Tiles",
"metadata": {
"custom-vector-tiles: version": "1.x"
},
"id": "us-constituencies",
"sources": {
"us-constituencies": {
"type": "vector",
"url": "http://localhost:3030/us-constituencies.json"
}
},
"layers": [
layerStyles['state-house'] // default style
]
};
var map = new mapboxgl.Map({
container: 'map', // container id
style,
center: [-97.04129450036908, 38.08064523709163], // starting position [lng, lat]
zoom: 3.5 // starting zoom
});
// toggle layers from legend clicks
$('.layer').click((e) => {
$(e.target).toggleClass('disabled');
const newLayers = [];
$.each($('.layer'), (_, el) => {
const $el = $(el);
if (!$el.hasClass('disabled')) {
const layer = $el.data('layer');
newLayers.push(layerStyles[layer]);
}
});
style.layers = newLayers;
map.setStyle(style);
});
// update debug on map movements
var debug = $('#debug');
function updateDebug() {
debug.html('Zoom: ' + map.getZoom());
}
map.on('zoom', updateDebug);
// Create a popup, but don't add it to the map yet.
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on('mousemove', function(e) {
var features = map.queryRenderedFeatures(e.point);
var showPopup = false;
if (features.length > 0) {
var feature = features[0];
var name = feature.properties['wof:name'];
if (name) {
map.getCanvas().style.cursor = 'pointer'
popup.setLngLat(e.lngLat).setHTML(name).addTo(map);
showPopup = true;
}
}
if (!showPopup) {
map.getCanvas().style.cursor = '';
popup.remove();
}
});
</script>
</body>
</html>