forked from UBC-GEOB472-Spring2020/mjrlab2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmjrlab2.html
652 lines (606 loc) · 16.8 KB
/
mjrlab2.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hospitals and Lifelabs in City of Vanouver </title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.8.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.8.1/mapbox-gl.css' rel='stylesheet' />
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 70%;
height: 90%
}
.Instructions {
position: absolute;
font-family: sans-serif;
margin-top: 40px;
margin-left: 5px;
padding: 5px;
width: 30%;
height: 40% border: 2px solid black;
font-size: 13px;
color: #222;
background-color: #fff;
border-radius: 3px;
}
</style>
</head>
<body>
<style>
.distance-container {
position: absolute;
top: 10px;
left: 10px;
z-index: 1;
}
.distance-container>* {
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
font-size: 11px;
line-height: 18px;
display: block;
margin: 0;
padding: 5px 10px;
border-radius: 3px;
}
</style>
<div id="map"></div>
<div id="distance" class="distance-container"></div>
<script src="https://npmcdn.com/@turf/[email protected]/turf.min.js"></script>
<div class='Instructions'>
<div><strong>instructions: You can click the Lifelabs icon and the nearest hospital will be displayed on the map. At the same time, the cursor becomes a crosshair on the map. After clicking on the map, a black dot will appear on the map, and you can click on the next point on the map to create a line that measures distance. The distance will show on the top left corner. This way, you can click on one of the life labs you are interested in, then connect it to the nearest hospital and see the distance between them. As you continue to click and draw points on this map, the distance measurement feature also applicable for multiple points and lines.If you want to redo the distance measurement, just click each point you clicked earlier, or simply refresh the site.</strong> <span id='mag'></span></div>
</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoianJtYSIsImEiOiJjazdrNDl0MmwwM3B6M2VtcjNhNDlvaWdqIn0._Q1MfouBwOZywgucwxn-ew';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/jrma/ck7k75grz5gjd1ik64rc45yrj',
center: [-123.15520, 49.25737],
zoom: 11
});
var hospitals = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {
Name: 'Urgent Care Centre and Sleep Disorders program -- UBC Hospital',
Address: '2211 Wesbrook Mall'
},
geometry: {
type: 'Point',
coordinates: [-123.245833, 49.264167]
}
},
{
type: 'Feature',
properties: {
Name: 'B.C. Women" Hospital & Health Centre',
Address: '4500 Oak St'
},
geometry: {
type: 'Point',
coordinates: [-123.1242, 49.24417]
}
},
{
type: 'Feature',
properties: {
Name: 'BC Cancer Agency',
Address: '600 W 10th Ave'
},
geometry: {
type: 'Point',
coordinates: [-123.1175, 49.2622222]
}
},
{
type: 'Feature',
properties: {
Name: 'British Columbia" Children Hospital',
Address: '4480 Oak St'
},
geometry: {
type: 'Point',
coordinates: [-123.1255556, 49.24444]
}
},
{
type: 'Feature',
properties: {
Name: 'Mount Saint Joseph Hospital',
Address: '3080 Prince Edward St'
},
geometry: {
type: 'Point',
coordinates: [-123.0952778, 49.2577778]
}
},
{
type: 'Feature',
properties: {
Name: 'St. Paul"s Hospital',
Address: '1081 Burrard St'
},
geometry: {
type: 'Point',
coordinates: [-123.1280556, 49.2805556]
}
},
{
type: 'Feature',
properties: {
Name: 'St. Vincent"s Hospitals Langara',
Address: '255 W 62nd Ave'
},
geometry: {
type: 'Point',
coordinates: [-123.110833, 49.214722]
}
},
{
type: 'Feature',
properties: {
Name: 'Sunny Hill Health Centre for Children',
Address: '3644 Slocan St'
},
geometry: {
type: 'Point',
coordinates: [-123.048333, 49.251944]
}
},
{
type: 'Feature',
properties: {
Name: 'Vancouver General Hospital',
Address: '899 W 12th Ave'
},
geometry: {
type: 'Point',
coordinates: [-123.123056, 49.261111]
}
},
{
type: 'Feature',
properties: {
Name: 'Burnaby General Hospital',
Address: '83935 Kincaid St'
},
geometry: {
type: 'Point',
coordinates: [-123.015, 49.249444]
}
},
{
type: 'Feature',
properties: {
Name: 'Riverview Hospital',
Address: '2601 Lougheed Hwy'
},
geometry: {
type: 'Point',
coordinates: [-122.805556, 49.246667]
}
},
{
type: 'Feature',
properties: {
Name: 'Delta Hospital',
Address: '5800 Mountain View Blvd'
},
geometry: {
type: 'Point',
coordinates: [-123.061111, 49.085556]
}
},
{
type: 'Feature',
properties: {
Name: 'Langley Memorial Hospital',
Address: '22051 Fraser Hwy'
},
geometry: {
type: 'Point',
coordinates: [-122.611944, 49.093611]
}
},
{
type: 'Feature',
properties: {
Name: 'Ridge Meadows Hospital & Health Care Centre',
Address: '11666 Laity St'
},
geometry: {
type: 'Point',
coordinates: [-122.629167, 49.215556]
}
},
{
type: 'Feature',
properties: {
Name: 'Royal Columbian Hospital',
Address: '330 E Columbia St'
},
geometry: {
type: 'Point',
coordinates: [-122.890278, 49.226389]
}
},
{
type: 'Feature',
properties: {
Name: 'Lions Gate Hospital',
Address: '231 15th St E'
},
geometry: {
type: 'Point',
coordinates: [-123.068333, 49.320833]
}
},
{
type: 'Feature',
properties: {
Name: 'Eagle Ridge Hospital',
Address: '475 Guildford Way'
},
geometry: {
type: 'Point',
coordinates: [-122.823333, 49.285]
}
},
{
type: 'Feature',
properties: {
Name: 'Richmond Hospital',
Address: '7000 Westminster Hwy'
},
geometry: {
type: 'Point',
coordinates: [-123.146667, 49.168889]
}
},
{
type: 'Feature',
properties: {
Name: 'Surrey Memorial Hospital',
Address: '13750 96 Ave'
},
geometry: {
type: 'Point',
coordinates: [-122.841667, 49.176111]
}
}
]
};
var lifelabs = libraries;
var libraries = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {
Name: 'Lifelabs Georgia PSC',
Address: '777 Hornby St Suite 835'
},
geometry: {
type: 'Point',
coordinates: [-12.9929884, 49.2256683]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs City Centre PSC',
Address: '1160 Burrard Street'
},
geometry: {
type: 'Point',
coordinates: [-123.1282851, 49.2799313]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Main & Keefer PSC',
Address: '180 Keefer St Suite 204'
},
geometry: {
type: 'Point',
coordinates: [-123.1008075, 49.2792620]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs East Hastings PSC',
Address: '1506 E. Hastings Street'
},
geometry: {
type: 'Point',
coordinates: [-123.0738220, 49.2811137]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Yaletown PSC',
Address: '136 Davie Street'
},
geometry: {
type: 'Point',
coordinates: [-123.1209565, 49.2731619]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Regent PSC',
Address: '2184 W. Broadway Suite 290'
},
geometry: {
type: 'Point',
coordinates: [-123.1549301, 49.2637159]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Fairmont PSC',
Address: '750 W. Broadway Suite 701'
},
geometry: {
type: 'Point',
coordinates: [-123.1211853, 49.2630228]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Commercial Drive PSC',
Address: '1750 E. 10th Avenue Suite 306'
},
geometry: {
type: 'Point',
coordinates: [-123.0685697, 49.2612137]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs King Edward PSC',
Address: '972 West King Edward Avenue'
},
geometry: {
type: 'Point',
coordinates: [-123.1256585, 49.2484025]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Woodridge PSC',
Address: '650 W. 41st Ave Suite 215'
},
geometry: {
type: 'Point',
coordinates: [-123.1186831, 49.2320524]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Victoria Drive PSC',
Address: '5786 Victoria Dr'
},
geometry: {
type: 'Point',
coordinates: [-123.0653900, 49.2315622]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Southill PSC',
Address: '6540 Fraser St'
},
geometry: {
type: 'Point',
coordinates: [-123.0903157, 49.2251213]
}
},
{
type: 'Feature',
properties: {
Name: 'Lifelabs Sunset PSC',
Address: '8207 Ontario Street'
},
geometry: {
type: 'Point',
coordinates: [-123.1071959, 49.2103893]
}
},
]
};
map.on('load', function() {
map.addLayer({
id: 'hospitals',
type: 'symbol',
source: {
type: 'geojson',
data: hospitals
},
layout: {
'icon-image': 'hospital-15',
'icon-allow-overlap': true
},
paint: {}
});
map.addLayer({
id: 'libraries',
type: 'symbol',
source: {
type: 'geojson',
data: libraries
},
layout: {
'icon-image': 'library-15'
},
paint: {}
});
map.addSource('nearest-hospital', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: []
}
});
});
var popup = new mapboxgl.Popup();
map.on('mousemove', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['hospitals', 'libraries']
});
if (!features.length) {
popup.remove();
return;
}
var feature = features[0];
popup.setLngLat(feature.geometry.coordinates)
.setHTML(feature.properties.Name)
.addTo(map);
map.getCanvas().style.cursor = features.length ? 'pointer' : '';
});
map.on('click', function(e) {
var libraryFeatures = map.queryRenderedFeatures(e.point, {
layers: ['libraries']
});
if (!libraryFeatures.length) {
return;
}
var libraryFeature = libraryFeatures[0];
var nearestHospital = turf.nearest(libraryFeature, hospitals);
if (nearestHospital !== null) {
map.getSource('nearest-hospital').setData({
type: 'FeatureCollection',
features: [nearestHospital]
});
map.addLayer({
id: 'nearest-hospital',
type: 'circle',
source: 'nearest-hospital',
paint: {
'circle-radius': 12,
'circle-color': '#486DE0'
}
}, 'hospitals');
}
});
var distanceContainer = document.getElementById('distance');
// GeoJSON object to hold our measurement features
var geojson = {
'type': 'FeatureCollection',
'features': []
};
// Used to draw a line between points
var linestring = {
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': []
}
};
map.on('load', function() {
map.addSource('geojson', {
'type': 'geojson',
'data': geojson
});
// Add styles to the map
map.addLayer({
id: 'measure-points',
type: 'circle',
source: 'geojson',
paint: {
'circle-radius': 5,
'circle-color': '#000'
},
filter: ['in', '$type', 'Point']
});
map.addLayer({
id: 'measure-lines',
type: 'line',
source: 'geojson',
layout: {
'line-cap': 'round',
'line-join': 'round'
},
paint: {
'line-color': '#000',
'line-width': 2.5
},
filter: ['in', '$type', 'LineString']
});
map.on('click', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['measure-points']
});
// Remove the linestring from the group
// So we can redraw it based on the points collection
if (geojson.features.length > 1) geojson.features.pop();
// Clear the Distance container to populate it with a new value
distanceContainer.innerHTML = '';
// If a feature was clicked, remove it from the map
if (features.length) {
var id = features[0].properties.id;
geojson.features = geojson.features.filter(function(point) {
return point.properties.id !== id;
});
} else {
var point = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [e.lngLat.lng, e.lngLat.lat]
},
'properties': {
'id': String(new Date().getTime())
}
};
geojson.features.push(point);
}
if (geojson.features.length > 1) {
linestring.geometry.coordinates = geojson.features.map(function(
point
) {
return point.geometry.coordinates;
});
geojson.features.push(linestring);
// Populate the distanceContainer with total distance
var value = document.createElement('pre');
value.textContent =
'Total distance: ' +
turf.length(linestring).toLocaleString() +
'km';
distanceContainer.appendChild(value);
}
map.getSource('geojson').setData(geojson);
});
});
map.on('mousemove', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['measure-points']
});
// UI indicator for clicking/hovering a point on the map
map.getCanvas().style.cursor = features.length ?
'pointer' :
'crosshair';
});
</script>
</body>
</html>