-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (122 loc) · 4.4 KB
/
index.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
class AccessibleMap {
constructor(options) {
this._layers = options.layers;
this._description = options.description;
}
onAdd(map) {
this._map = map;
this._mapcontainer = map._container;
this._mapcontainer.setAttribute('aria-label', this._description + 'Use the directional arrows to explore points of interest on the map.')
this._mapcontainer.setAttribute('aria-live', 'assertive')
this._container = document.createElement('div');
this._container.className = 'maplibre-accessibility-ctrl';
const featureSelector = document.createElement('div');
featureSelector.classList.add("feature-selector");
featureSelector.style.position = 'fixed';
featureSelector.style.borderStyle = 'solid';
featureSelector.style.borderWidth = '5px';
featureSelector.style.borderRadius = '5px';
featureSelector.style.top = '50%';
featureSelector.style.left = '50%';
featureSelector.style.marginTop = '-100px';
featureSelector.style.marginLeft = '-100px';
featureSelector.style.width = '200px';
featureSelector.style.height = '200px';
this._container.appendChild(featureSelector);
const features = document.createElement('div');
features.setAttribute('id', 'features')
features.setAttribute('aria-live', 'assertive');
features.style.fontSize = '22px';
features.style.width = '400px';
features.style.height = '50vh';
features.style.marginRight = '0';
features.style.marginLeft = 'auto';
features.style.backgroundColor = 'white';
this._container.appendChild(features);
this._map.on("moveend", this.moveEnd.bind(this));
this._map.once("idle", this.idleMap.bind(this));
return this._container;
}
onRemove() {
this._container.parentNode?.removeChild(this._container);
this._map = undefined;
}
displayProperties() {
var point = this._map.project(this._map.getCenter());
var width = 200;
var height = 200;
var features = this._map.queryRenderedFeatures([
[point.x - width / 2, point.y - height / 2],
[point.x + width / 2, point.y + height / 2]
], { layers: this._layers });
features.map(function (feat, i) {
document.getElementById('features').innerHTML += '<div class="featureProperties" arialabel= "' + feat.properties.name + '" tabindex="' + (i + 2) + '">' + (i + 1) + '. ' + feat.properties.name + '</div>'
document.getElementsByClassName('featureProperties')[i].style.outline = '2px solid black';
document.getElementsByClassName('featureProperties')[i].style.padding = '20px';
document.getElementsByClassName('featureProperties')[i].style.borderRadius = '5px';
});
}
bindDirectionKeys() {
var bearing = this._map.getBearing()
document.onkeydown = (event) => {
switch (event.keyCode) {
//West
case 37:
document.getElementById('features').innerHTML = 'Moving ' + this.bearingToDirection(bearing - 90)
break;
//North
case 38:
document.getElementById('features').innerHTML = 'Moving ' + this.bearingToDirection(bearing)
break;
//East
case 39:
document.getElementById('features').innerHTML = 'Moving ' + this.bearingToDirection(bearing + 90)
break;
//South
case 40:
document.getElementById('features').innerHTML = 'Moving ' + this.bearingToDirection(bearing + 180)
break;
}
};
}
bearingToDirection(bearing){
var positiveBearing = this.makeBearingPositive(bearing)
switch (true) {
case (positiveBearing < 30):
return "North";
case (positiveBearing <= 60):
return "Northeast";
case (positiveBearing <= 120):
return "East";
case (positiveBearing <= 150):
return "Southeast";
case (positiveBearing <= 210):
return "South";
case (positiveBearing <= 240):
return "Southwest";
case (positiveBearing <= 300):
return "West";
case (positiveBearing <= 330):
return "Northwest"
case (positiveBearing <= 360):
return "North"
default:
console.log('Default');
}
}
makeBearingPositive(bearing) {
if (bearing < 0) {
return bearing += 360
} else {
return bearing
}
}
moveEnd() {
document.getElementById('features').innerHTML = '';
this.displayProperties();
}
idleMap() {
this.displayProperties();
this.bindDirectionKeys();
}
}