forked from findthemasks/findthemasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locations-list-map.js
405 lines (351 loc) · 12.9 KB
/
locations-list-map.js
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
function toDataByLocation(data) {
const headers = data.values[0];
const approvedIndex = headers.findIndex( e => e === 'Approved' );
const stateIndex = headers.findIndex( e => e === 'State?' );
const cityIndex = headers.findIndex( e => e === 'City' );
const data_by_location = {};
const published_entries = data.values.slice(1).filter((entry) => entry[approvedIndex] === "x");
published_entries.forEach( entry => {
const state = entry[stateIndex];
const city = entry[cityIndex];
let entry_array;
if (!(state in data_by_location) || !(city in data_by_location[state])) {
entry_array = [];
if (state in data_by_location) {
data_by_location[state][city] = entry_array;
} else {
data_by_location[state] = { [city]: entry_array };
}
} else {
entry_array = data_by_location[state][city];
}
const entry_obj = {};
headers.forEach( (value, index) => {
if (entry[index] !== undefined) {
entry_obj[value] = entry[index]
} else {
entry_obj[value] = ""
}
});
entry_array.push(entry_obj);
});
return data_by_location;
}
function createFiltersListHTML() {
// We use objects here as a quick approach to removing duplicates.
const states = {};
const acceptOpenFilters = {};
for (const state of Object.keys(data_by_location).sort()) {
states[state] = true;
const cities = data_by_location[state];
for (const city of Object.keys(cities).sort()) {
for (const entry of cities[city]) {
const v = entry["Will they accept open boxes/bags?"];
acceptOpenFilters[toHTMLID(v)] = v;
}
}
}
const filters = [];
filters.push(`<h4>States</h4>`);
for (const state of Object.keys(states)) {
filters.push(`
<div>
<input
id="state-${state}"
type="checkbox"
name="states"
value="${state}"
onchange="onFilterChange(this)"
/>
<label
id="state-${state}-label"
for="state-${state}"
>
${state}
</label>
</div>
`);
}
// filters.push(`<h3>Accepts Open Boxes/bags</h3>`);
// for (const id of Object.keys(acceptOpenFilters)) {
// const val = acceptOpenFilters[id];
// filters.push(`
// <div>
// <input
// id="accept-open-${id}"
// type="checkbox"
// name="accept-open"
// value="${id}"
// onchange="onFilterChange(this)"
// />
// <label
// id="accept-open-${id}-label"
// for="accept-open-${id}"
// >
// ${val}
// </label>
// </div>
// `);
// }
const acceptedItemsFilter = {
'n95s': 'N95 masks/respirators',
'masks': 'surgical masks',
'face shields': 'face shields',
'booties': 'medical booties',
'goggles': 'safety goggles',
'gloves': 'gloves',
'kleenex': 'kleenex',
'sanitizer': 'hand-sanitizer',
'overalls': 'medical overalls',
'gowns': 'gowns',
'respirators': 'advanced respirators (PAPR/CAPR/etc.)',
};
filters.push(`<h4>Accepted Items</h4>`);
for (const val of Object.keys(acceptedItemsFilter)) {
const id = toHTMLID(val);
const descr = acceptedItemsFilter[val];
filters.push(`
<div>
<input
id="accept-item-${id}"
type="checkbox"
name="accept-item"
value="${val}"
onchange="onFilterChange(this)"
/>
<label
id="accept-item-${id}-label"
for="accept-item-${id}"
>
${descr}
</label>
</div>
`);
}
return filters;
}
function toHTMLID(name) {
let s = '';
for (let i = 0; i < name.length; i++) {
let c = name.charAt(i);
// We remove `.` from IDs because selection using jQuery failed when they
// appeared in IDs. TODO This should be investigated further.
if (c.match(/^[a-z0-9_:]+$/i)) {
s += c;
} else {
s += '-';
}
}
return s.toLowerCase();
}
function toHtmlSnippets(data_by_location, filters) {
const lines = [];
for (const state of Object.keys(data_by_location).sort()) {
if (filters && filters.states && !filters.states[state]) {
continue;
}
const cityLines = [];
const cities = data_by_location[state];
for (const city of Object.keys(cities).sort()) {
const entryLines = [];
for (const entry of cities[city]) {
const name = entry["What is the name of the hospital or clinic?"];
const address = entry["Street address for dropoffs?"];
const instructions = entry["Drop off instructions, eg curbside procedure or mailing address ATTN: instructions:"];
const accepting = entry["What are they accepting?"];
const will_they_accept = entry["Will they accept open boxes/bags?"];
if (filters) {
// if (filters.acceptOpens && !filters.acceptOpens[toHTMLID(will_they_accept)]) {
// continue;
// }
if (filters.acceptItems) {
let acc = accepting.toLowerCase();
if (!Object.keys(filters.acceptItems).some(s => acc.includes(s))) {
continue;
}
}
}
entryLines.push(`<div class=location>`)
entryLines.push(`<h4 class="marginBottomZero">${name}</h4>`);
entryLines.push(`<label>Address</label>`)
entryLines.push(`<p class="marginTopZero medEmph">${address.replace(/\n/g,'<br>')}</p>`);
if (instructions !== "") {
entryLines.push(`<label>Instructions</label>`)
entryLines.push(`<p>${instructions}</p>`);
}
if (accepting !== "") {
entryLines.push(`<label>Accepting</label>`)
entryLines.push(`<p>${accepting}</p>`);
}
if (will_they_accept !== "") {
entryLines.push(`<label>Open packages?</label>`)
entryLines.push(`<p>${will_they_accept}</p>`);
}
entryLines.push('</div>');
}
if (entryLines.length > 0) {
cityLines.push(`<div class=city>`)
cityLines.push(`<h3>${city}</h3>`);
cityLines.push(entryLines.join('\n'));
cityLines.push('</div>');
}
}
if (cityLines.length > 0) {
lines.push(`<div class=state>`);
lines.push(`<h2>${state}</h2>`);
lines.push(cityLines.join('\n'));
lines.push('</div>');
}
}
return lines;
}
document.addEventListener("DOMContentLoaded", function() {
$.getJSON("https://findthemasks.com/data.json", function(result){
// may end up using this for search / filtering...
window.locations = result;
window.data_by_location = toDataByLocation(locations);
initMap();
$(".filters-list").html(createFiltersListHTML(data_by_location).join(" "));
const htmlSnippets = toHtmlSnippets(data_by_location, null);
$(".locations-list").html(htmlSnippets.join(" "));
});
});
function onFilterChange(elem) {
// This is a hacky approach to programatically highlighting selected items as
// it uses hard-coded ID references. We use this approach for now for
// simplicity, speed of implementation and performance, but it should ideally
// be replaced with a more robust solution if time allows and performance
// isn't affected.
let label = $("#" + elem.id + "-label");
if (elem.checked) {
label.addClass("selected");
} else {
label.removeClass("selected");
}
let states = null;
document.filters['states'].forEach((state) => {
if (state.checked) {
if (states === null) {
states = {};
}
states[state.value] = true;
}
});
// let acceptOpens = null;
// document.filters['accept-open'].forEach((acceptOpen) => {
// if (acceptOpen.checked) {
// if (acceptOpens === null) {
// acceptOpens = {};
// }
// acceptOpens[acceptOpen.value] = true;
// }
// });
let acceptItems = null;
document.filters['accept-item'].forEach((acceptItem) => {
if (acceptItem.checked) {
if (acceptItems === null) {
acceptItems = {};
}
acceptItems[acceptItem.value] = true;
}
});
const filters = {states, acceptItems};
const htmlSnippets = toHtmlSnippets(window.data_by_location, filters);
$(".locations-list").html(htmlSnippets.join(" "));
}
function initMap() {
var data_by_location = window.data_by_location;
var middle_of_us = { lat: 39.0567939, lng: -94.6065124};
var element = document.getElementById('map');
if (element == null) {
alert('could not find map div');
}
// The map, roughly zoomed to show the entire US.
var map = new google.maps.Map( element, {zoom: 4, center: middle_of_us});
var markers = [];
var i = 0;
for (const state of Object.keys(data_by_location).sort()) {
const cities = data_by_location[state];
for (const city of Object.keys(cities).sort()) {
for (const entry of cities[city]) {
const name = entry["What is the name of the hospital or clinic?"];
const address = entry["Street address for dropoffs?"];
const latitude = entry["Lat"];
const longitude = entry["Lng"];
const instructions = entry["Drop off instructions, eg curbside procedure or mailing address ATTN: instructions:"];
const accepting = entry["What are they accepting?"];
const open_accepted = entry["Will they accept open boxes/bags?"];
// Convert the lat and lng fields to numbers
if (!isNaN(Number(latitude))) {
var marker = addMarkerToMap(map, Number(latitude), Number(longitude),
address, name, instructions, accepting, open_accepted);
markers.push(marker);
}
}
}
}
// Center the map on the nearest markers to the user if possible
centerMapToNearestMarkers(map, markers);
}
function centerMapToNearestMarkers(map, markers) {
// First check to see if the user will accept getting their location, if not, silently return
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var user_latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//Compute the distances of all markers from the user
var markerDistances = new Map(); // an associative array containing the marker referenced by the computed distance
var distances = []; // all the distances, so we can sort and then call markerDistances
for (const marker of markers) {
var distance = google.maps.geometry.spherical.computeDistanceBetween(marker.position, user_latlng);
// HACK: In the unlikely event that the exact same distance is computed, add one meter to the distance to give it a unique distance
// This could occur if a marker was added twice to the same location.
if (markerDistances.has(distance)) { distance = distance + 1; }
markerDistances[distance] = marker;
distances.push(distance);
}
// sort the distances and set bounds to closest three
distances.sort((a, b) => a - b);
// center the map on the user
bounds = new google.maps.LatLngBounds();
bounds.extend(user_latlng);
// Extend the bounds to contain the three closest markers
i = 0;
while (i < 3) {
// Get one of the closest markers
var distance = distances[i]
var marker = markerDistances[distance];
// Add to the iterator first just in case something fails later to avoid infinite loop
i++;
marker_lat = marker.position.lat();
marker_lng = marker.position.lng();
loc = new google.maps.LatLng(marker_lat, marker_lng);
bounds.extend(loc);
}
map.fitBounds(bounds); // auto-zoom
map.panToBounds(bounds); // auto-center
})
}
}
function addMarkerToMap(map, latitude, longitude, address, name, instructions, accepting, open_accepted) {
// Text to go into InfoWindow
var contentString =
'<h5>' + name + '</h5>' +
'<div class=label>Address:</div><div class=value>' + address + '</div>' +
'<div class=label>Instructions:</div><div class=value>' + instructions + '</div>' +
'<div class=label>Accepting:</div><div class=value>' + accepting + '</div>' +
'<div class=label>Open Packages?:</div><div class=value>' + open_accepted + '</div>';
// InfoWindow will pop up when user clicks on marker
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var location = { lat: latitude, lng: longitude };
var marker = new google.maps.Marker({
position: location,
title: name,
map: map
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
return marker;
}