-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
507 lines (467 loc) · 19.2 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>COVID-19 Testing Locations | San Francisco</title>
<meta name='robots' content='noindex, nofollow'>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<link rel="stylesheet" media="all" href="//fonts.googleapis.com/css?family=Rubik:300,400,500,700">
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.css' rel='stylesheet' />
<link rel="stylesheet" media="all" href="/index.css" />
<!-- Mapbox -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.js'></script>
<!-- Geocoder plugin -->
<script
src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet'
href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.css'
type='text/css' />
<link rel="stylesheet" media="all" href="index.css" />
<!-- Turf.js plugin -->
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
}); var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-NLN95H2');</script>
<!-- End Google Tag Manager -->
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-NLN95H2" height="0" width="0"
style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<div class='sidebar'>
<div class='heading'>
<h1>COVID-19 Testing Locations</h1>
<div>Read more <a href="https://sf.gov/GetTestedSF">about getting tested in San Francisco</a></div>
</div>
<div id='listings-container'>
<div id='filters' class='filters'>
<div class="switch-field">
<div class="switch-title"><strong>Filter Locations</strong></div>
<div class="switch-input" data-children-count="2">
<input id="filter-all-sites" type="radio" name="maptoggle" value="all" checked="checked"><label
id="label-all-sites" for="filter-all-sites">All Sites</label><input id="filter-public-sites" type="radio"
name="maptoggle" value="public"><label class="label-icon" id="label-public-sites" for="filter-public-sites">No Insurance
Required</label>
</div>
</div>
<div class="switch-field">
<div class="switch-title"><strong>Sort Locations</strong></div>
<div class="switch-input" data-children-count="2">
<input id="sort-alpha" type="radio" name="sites" value="alpha" checked="checked"><label id="label-alpha"
for="sort-alpha">Alphabetically</label><input id="sort-closest" type="radio" name="sites"
value="closest"><label id="label-closest" for="sort-closest">Closest to Me</label>
</div>
</div>
</div>
<div id='listings' class='listings'></div>
</div>
</div>
<div id='map' class='map'></div>
<script>
// This will let you use the .remove() function later on
if (!('remove' in Element.prototype)) {
Element.prototype.remove = function () {
if (this.parentNode) {
this.parentNode.removeChild(this);
}
};
}
mapboxgl.accessToken = 'pk.eyJ1IjoiZGF0YXNmIiwiYSI6ImNrOXB4aGpxczBib2szam1vdTdvZ2tlZTIifQ.Jo2x1aJbnvoOqb5z0f5A4Q';
var bounds = [ -122.517910874663, 37.6044780500533, -122.354995082683, 37.8324430069081];
/**
* Add the map to the page
*/
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
center: [-122.431297, 37.773972],
zoom: 12,
pitchWithRotate: false,
maxBounds: bounds
});
var geolocate = new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: false
});
map.addControl(geolocate, 'top-left');
var dataUrl = "https://data.sfgov.org/resource/dtit-7gp4.geojson?$where=point+is+not+null"
let locations;
fetch(dataUrl)
.then(function(response) {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(function(data) {
locations = data;
return;
});
var filterState = ['Public', 'Private']
/**
* Assign a unique id to each location. You'll use this `id`
* later to associate each point on the map with a listing
* in the sidebar.
//location id added in source data
locations.features.forEach(function (location, i) {
location.properties.id = i;
});
*/
/**
* Wait until the map loads to make changes to the map.
*/
map.on('load', function (e) {
/**
add geocoder
**/
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken, // Set the access token
mapboxgl: mapboxgl, // Set the mapbox-gl instance
marker: true, // Use the geocoder's default marker style
bbox: [-122.6001, 37.6403, -122.2818, 37.8481] // Set the bounding box coordinates
});
map.addControl(geocoder, 'top-left');
map.addControl(new mapboxgl.NavigationControl({
showCompass: false
}));
// disable map rotation using right click + drag
map.dragRotate.disable();
// disable map rotation using touch rotation gesture
map.touchZoomRotate.disableRotation();
/**
* This is where your '.addLayer()' used to be, instead
* add only the source without styling a layer
*/
map.addSource("places", {
"type": "geojson",
"data": dataUrl
});
geolocate.on('geolocate', function (ev) {
var geom = {
geometry: {
coordinates: [ev.coords.longitude, ev.coords.latitude],
type: "Point"
}
};
reSortList(geom, filterState);
})
/**
* Listen for when a geocoder result is returned. When one is returned:
* - Calculate distances
* - Sort locations by distance
* - Rebuild the listings
* - Adjust the map camera
* - Open a popup for the closest location
* - Highlight the listing for the closest location.
*/
geocoder.on('result', function (ev) {
reSortList(ev.result, filterState);
});
function reBuildListings(locations) {
/**
* Rebuild the listings:
* Remove the existing listings and build the location
* list again using the newly sorted locations.
*/
var listings = document.getElementById('listings');
while (listings.firstChild) {
listings.removeChild(listings.firstChild);
}
buildLocationList(locations, filterState);
}
function reSortList(geom, filter) {
/**
* Calculate distances:
* For each location, use turf.disance to calculate the distance
* in miles between the searchResult and the location. Assign the
* calculated value to a property called `distance`.
*/
var filter = filter ? filter : ['Private', 'Public']
var options = { units: 'miles' };
locations.features.forEach(function (location) {
if (filter.includes(location.properties["location_type"])) {
Object.defineProperty(location.properties, 'distance', {
value: turf.distance(geom, location.geometry, options),
writable: true,
enumerable: true,
configurable: true
});
} else {
Object.defineProperty(location.properties, 'distance', {
value: 1000,
writable: true,
enumerable: true,
configurable: true
})
}
});
/**
* Sort locations by distance from closest to the `searchResult`
* to furthest.
*/
locations.features.sort(function (a, b) {
if (a.properties.distance > b.properties.distance) {
return 1;
}
if (a.properties.distance < b.properties.distance) {
return -1;
}
return 0; // a must be equal to b
});
reBuildListings(locations);
/* Open a popup for the closest location. */
createPopUp(locations.features[0]);
/** Highlight the listing for the closest location. */
var activeListing = document.getElementById('listing-' + locations.features[0].properties.id);
activeListing.classList.add('active');
/**
* Adjust the map camera:
* Get a bbox that contains both the geocoder result and
* the closest location. Fit the bounds to that bbox.
*/
var bbox = getBbox(locations, 0, geom);
map.fitBounds(bbox, {
padding: 100
});
};
function sortAlpha(locations) {
locations.features.sort(function (a, b) {
if (a.properties.name < b.properties.name) { return -1; }
if (a.properties.name > b.properties.name) { return 1; }
return 0;
})
reBuildListings(locations);
};
/**
* Using the coordinates (lng, lat) for
* (1) the search result and
* (2) the closest location
* construct a bbox that will contain both points
*/
function getBbox(sortedlocations, locationIdentifier, geom) {
var lats = [sortedlocations.features[locationIdentifier].geometry.coordinates[1], geom.geometry.coordinates[1]]
var lons = [sortedlocations.features[locationIdentifier].geometry.coordinates[0], geom.geometry.coordinates[0]]
var sortedLons = lons.sort(function (a, b) {
if (a > b) { return 1; }
if (a.distance < b.distance) { return -1; }
return 0;
});
var sortedLats = lats.sort(function (a, b) {
if (a > b) { return 1; }
if (a.distance < b.distance) { return -1; }
return 0;
});
return [
[sortedLons[0], sortedLats[0]],
[sortedLons[1], sortedLats[1]]
];
}
/**
* Add all the things to the page:
* - The location listings on the side of the page
* - The markers onto the map
*/
var noInsurance = document.getElementById('label-public-sites');
noInsurance.addEventListener('click', function (e) {
filterState = ['Public'];
let markers = document.getElementsByClassName("marker-private");
for (let i = 0; i < markers.length; i++) {
markers[i].style.visibility = "hidden";
}
let listings = document.getElementsByClassName("listing-item__private");
for (let i = 0; i < listings.length; i++) {
listings[i].style.display = "none";
}
});
var allSites = document.getElementById('label-all-sites');
allSites.addEventListener('click', function (e) {
filterState = ['Public', 'Private'];
let markers = document.getElementsByClassName("marker");
for (let i = 0; i < markers.length; i++) {
markers[i].style.visibility = "visible";
}
let listings = document.getElementsByClassName("listing-item");
for (let i = 0; i < listings.length; i++) {
listings[i].style.display = "block";
}
});
var alpha = document.getElementById('label-alpha')
alpha.addEventListener('click', function (e) {
return sortAlpha(locations);
});
var closest = document.getElementById('label-closest')
closest.addEventListener('click', function (e) {
return geolocate.trigger();
});
sortAlpha(locations);
//buildLocationList(locations);
addMarkers();
})
/**
* Add a marker to the map for every location listing.
**/
function addMarkers() {
/* For each feature in the GeoJSON object above: */
locations.features.forEach(function (marker) {
/* Create a div element for the marker. */
var el = document.createElement('div');
/* Assign a unique `id` to the marker. */
el.id = "marker-" + marker.properties.id;
/* Assign the `marker` class to each marker for styling. */
el.className = 'marker';
if (marker.properties.location_type == "Public") {
el.className += ' marker-public'
} else {
el.className += ' marker-private'
}
/**
* Create a marker using the div element
* defined above and add it to the map.
**/
new mapboxgl.Marker(el, { offset: [0, -23] })
.setLngLat(marker.geometry.coordinates)
.addTo(map);
/**
* Listen to the element and when it is clicked, do three things:
* 1. Fly to the point
* 2. Close all other popups and display popup for clicked location
* 3. Highlight listing in sidebar (and remove highlight for all other listings)
**/
el.addEventListener('click', function (e) {
/* Fly to the point */
flyTolocation(marker);
/* Close all other popups and display popup for clicked location */
createPopUp(marker);
/* Highlight listing in sidebar */
var activeItem = document.getElementsByClassName('active');
e.stopPropagation();
if (activeItem[0]) {
activeItem[0].classList.remove('active');
}
var listing = document.getElementById('listing-' + marker.properties.id);
listing.classList.add('active');
listing.scrollIntoView();
});
});
}
/**
* Add a listing for each location to the sidebar.
**/
function buildLocationList(data, filter) {
var filter = filter ? filter : ['Public', 'Private']
let locations = data.features.filter(function (location) {
return filter.includes(location.properties["location_type"])
});
locations.forEach(function (location, i) {
/**
* Create a shortcut for `location.properties`,
* which will be used several times below.
**/
var prop = location.properties;
/* Add a new listing section to the sidebar. */
var listings = document.getElementById('listings');
var listing = listings.appendChild(document.createElement('div'));
/* Assign a unique `id` to the listing. */
listing.id = "listing-" + prop.id;
/* Assign the `item` class to each listing for styling. */
listing.className = 'listing-item ';
listing.className += location.properties.location_type == 'Public' ? 'listing-item__public' : 'listing-item__private'
/* Add the link to the individual listing created above. */
var link = listing.appendChild(document.createElement('a'));
link.href = '#';
link.className = 'title';
link.id = "link-" + prop.id;
link.innerHTML = prop.name;
if (prop.distance) {
var roundedDistance = Math.round(prop.distance * 100) / 100;
listing.prepend(roundedDistance + ' miles away')
}
/* Add details to the individual listing. */
var details = listing.appendChild(document.createElement('div'));
details.innerHTML = prop.address + "<br/>San Francisco, CA";
/*if (prop.phone_number_formatted) {
details.innerHTML += '<br/>' + prop.phone_number_formatted;
}*/
if (prop.website) {
details.innerHTML += '<br/><a href="' + prop.website + '\">' + prop.website + '</a>'
}
if (prop.eligibility) {
details.innerHTML += '<p><strong>Eligibility:</strong> ' + prop.eligibility + '</p>';
}
if (prop.testing_hours) {
details.innerHTML += '<p><strong>Hours:</strong> ' + prop.testing_hours + '</p>';
}
if (prop.cta_link) {
details.innerHTML += '<div class="sfgov-cta-button__container"><div class="field field--type-link __link field__item"><a class="cta-link" href="' + prop.cta_link + '">' + prop.cta_text + '</a></div></div>';
} else {
details.innerHTML += '<p><strong>' + prop.cta_text + '</strong></p>';
}
if (prop.cta_link && prop.cta_link.includes('tel')) {
details.innerHTML += '<p><strong>Phone:</strong> ' + prop.phone_number_formatted + '</p>';
}
if (prop.medical_home) {
details.innerHTML += 'Testing provided by ' + prop.medical_home + '</p>';
}
/**
* Listen to the element and when it is clicked, do four things:
* 1. Update the `currentFeature` to the location associated with the clicked link
* 2. Fly to the point
* 3. Close all other popups and display popup for clicked location
* 4. Highlight listing in sidebar (and remove highlight for all other listings)
**/
link.addEventListener('click', function (e) {
for (var i = 0; i < data.features.length; i++) {
if (this.id === "link-" + data.features[i].properties.id) {
var clickedListing = data.features[i];
flyTolocation(clickedListing);
createPopUp(clickedListing);
}
}
var activeItem = document.getElementsByClassName('active');
if (activeItem[0]) {
activeItem[0].classList.remove('active');
}
this.parentNode.classList.add('active');
});
});
}
/**
* Use Mapbox GL JS's `flyTo` to move the camera smoothly
* a given center point.
**/
function flyTolocation(currentFeature) {
map.flyTo({
center: currentFeature.geometry.coordinates,
zoom: 15
});
}
/**
* Create a Mapbox GL JS `Popup`.
**/
function createPopUp(currentFeature) {
var popUps = document.getElementsByClassName('mapboxgl-popup');
if (popUps[0]) popUps[0].remove();
var cta = currentFeature.properties.cta_link ? '<div class="sfgov-cta-button__container"><div class="field field--type-link __link field__item"><a class="cta-link" href="' + currentFeature.properties.cta_link + '">' + currentFeature.properties.cta_text + '</a></div></div>' : '<div><strong>' + currentFeature.properties.cta_text + '</strong></div>'
var phone = (currentFeature.properties.cta_link && currentFeature.properties.cta_link.includes('tel')) ?
'<p><strong>Phone:</strong> ' + currentFeature.properties.phone_number_formatted + '</p>' : '';
var popup = new mapboxgl.Popup({ closeOnClick: true })
.setLngLat(currentFeature.geometry.coordinates)
.setHTML('<h3>' + currentFeature.properties.name + '</h3>' +
'<h4>' + currentFeature.properties.address + '</h4>' +
'<p><strong>Eligibility:</strong> ' + currentFeature.properties.eligibility + '</p>' +
'<p><strong>Hours:</strong> ' + currentFeature.properties.testing_hours + '</p>' +
cta + phone)
.addTo(map);
}
</script>
</body>
</html>