-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
330 lines (305 loc) · 12.9 KB
/
js.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
const accessKey = "pk.eyJ1Ijoic2VhbmJyb29rZXIiLCJhIjoiY2x0b3ZzYjA5MGtvZzJqcGNrb3g1d3l4aSJ9.9p072aD5fFi4HG5YuWHI6A";
const zoom_extents = { '10': 17, '25': 16, '50': 15, '_1': 14 }
var modal, map, dark, train_stations, zoomed_in = false;
async function getData(source, sourceLayer) {
return new Promise((resolve, reject) => {
try {
var features;
if (!map.isSourceLoaded(source)) {
console.log("Waiting for load");
map.on('sourcedata', function checkData(e) {
if (e.sourceId==source && e.isSourceLoaded) {
console.log("Loaded");
setTimeout(()=>{
features = map.querySourceFeatures(source, { sourceLayer: sourceLayer });
map.off('sourcedata', checkData);
resolve(features)
}, 500);
}
})
} else {
console.log("Else");
features = map.querySourceFeatures(source, { sourceLayer: sourceLayer });
resolve(features)
}
} catch(err) {
reject(err);
}
});
}
function loadSymbols(name) {
return new Promise((resolve, reject) => {
map.loadImage(`https://raw.githubusercontent.com/Byrix/parknear/main/symbols/${name}.png`, (err, image) => {
if (err) {
reject(err);
} else {
map.addImage(`symbol-${name}`, image);
resolve();
}
})
});
}
async function loadData() {
// Load all custom symbology used
await Promise.all(['parking', 'parking-dark', 'train', 'parking-onstreet-dark', 'parking-onstreet'].map((name) => loadSymbols(name)))
.then(() => console.log('All symbols loaded!'))
.catch(err => console.warn("ERROR: " + err));
// Add sources and layers
map.addSource('parking-source', {
'type': 'vector',
'url': 'mapbox://seanbrooker.0eu87qns'
}).addLayer({
'id': 'parking',
'type': 'symbol',
'source': 'parking-source',
'source-layer': 'parking-8wthl3',
// 'maxzoom': 16, // Only display when not zoomed in
'layout': {
// 'icon-image': dark ? 'symbol-parking' : 'symbol-parking-dark',
'icon-image': ['case',
['==', 0, ['get', 'street']],
dark ? 'symbol-parking' : 'symbol-parking-dark',
dark ? 'symbol-parking-onstreet' : 'symbol-parking-onstreet-dark',
],
'icon-size': 0.2,
'symbol-sort-key': ['-', ['get', 'capacity']], // Prioritise display of high capacity car parks
}
});
// map.addSource('parking-poly-source', {
// 'type': 'vector',
// 'url': 'mapbox://seanbrooker.792feceu'
// }).addLayer({
// 'id': 'parking-poly',
// 'type': 'fill',
// 'source': 'parking-poly-source',
// 'source-layer': 'parking-poly-merc-0fc86v',
// 'minzoom': 16, // Only display when zoomed in
// 'paint': {
// 'fill-color': dark ? '#3b4252' : '#e5e9f0',
// 'fill-outline-color': dark ? '#e5e9f0' : '#3b4252'
// }
// });
map.addSource('station-source', {
'type': 'vector',
'url': 'mapbox://seanbrooker.5sd4zdao'
}).addLayer({
'id': 'stations',
'type': 'symbol',
'source': 'station-source',
'source-layer': 'stations-5p00fr',
'layout': {
'icon-image': 'symbol-train',
'icon-size': 0.3
}
});
}
async function refresh() {
console.log("Refreshing");
$('*').css('cursor', 'progress');
// Get new filter values
let dist = $('input[name="distance"]:checked').val();
let station = $('#select_station').val();
let street = $('input[name="street"]:checked').val();
if (!dist || !station || !street) return;
await Promise.all([
(async () => {
// Update filters
// Define feature filters
console.log("refresh() : updating filters");
var filterTerms = ['all'];
filterTerms.push(['in', station, ['get', `station_${dist}`]]);
if(street !== undefined && street!=='3') { filterTerms.push(['==', parseInt(street), ['get', 'street']]) }
// Update the filters
map.setFilter('parking', filterTerms);
// map.setFilter('parking-poly', filterTerms);
console.log('refresh() : new filters set');
})(),
(async () => {
// Update map view
// Get all stations
console.log('refresh() : getting map data');
const stations = await getData('station-source', 'stations-5p00fr');
let stationSub = station.substring(0, station.length-1);
let stationLocation;
stations.forEach(st => {
let sterm = st.properties.sterm;
if (sterm===stationSub) { stationLocation = new mapboxgl.LngLat(st.properties.LONGITUDE, st.properties.LATITUDE); }
});
if (stationLocation) { map.flyTo({center: stationLocation, zoom: zoom_extents[dist]}); }
console.log('refresh() : updated map');
})()
]);
$('*').css('cursor', '');
console.log('refresh() : refreshed')
console.log("refresh() : check for results");
const getRendered = async (layer) => {
return new Promise((resolve, reject) => {
try {
if (!map.style || !map.isStyleLoaded()) {
map.once('idle', () => {
resolve(map.queryRenderedFeatures({ layers: [layer] }));
})
} else {
resolve(map.queryRenderedFeatures({ layers: [layer] }));
}
} catch(err) {
reject(err);
}
})
};
renderedFeatures = await getRendered('parking');
if (renderedFeatures.length==0) {
alert("No carparks found for the given specifications!");
}
}
async function initMap() {
// Load data into the map
loadData().then(() => console.log("Data loaded!")).then( async () => {
var $dropdown = $('#select_station')
var stations = await getData('station-source', 'stations-5p00fr');
train_stations = stations;
stations.forEach(station => {
$select = $(`<option value='${station.properties.sterm},'>${station.properties.name}</option>`);
$dropdown.append($select);
});
$dropdown.val('');
});
// Add map controls
map.addControl(new mapboxgl.NavigationControl())
.addControl(new mapboxgl.FullscreenControl())
.addControl(new mapboxgl.ScaleControl());
// Customise map controls
['zoom-in', 'zoom-out', 'compass', 'fullscreen'].forEach(btnname => {
const $btn = $(`.mapboxgl-ctrl button.mapboxgl-ctrl-${btnname} .mapboxgl-ctrl-icon`);
$btn.addClass('material-symbols-outlined');
});
$(`.mapboxgl-ctrl button.mapboxgl-ctrl-zoom-in .mapboxgl-ctrl-icon`).append('add');
$(`.mapboxgl-ctrl button.mapboxgl-ctrl-zoom-out .mapboxgl-ctrl-icon`).append('remove');
$(`.mapboxgl-ctrl button.mapboxgl-ctrl-fullscreen .mapboxgl-ctrl-icon`).append('fullscreen');
$(`.mapboxgl-ctrl button.mapboxgl-ctrl-compass .mapboxgl-ctrl-icon`).append('explore');
// MOUSE EVENTS
// Parking events
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false,
});
const mouseenterparking = (e) => {
map.getCanvas().style.cursor = 'pointer';
// console.log(e);
let properties = e.features[0].properties;
// console.log(properties);
let street_text = properties.street===1 ? 'On-Street Parking' : 'Off-street Parking';
let newHTML = `<b>${street_text}</b><br><b>Capacity: </b>${properties.capacity}<br><br>`
newHTML += `<span class='text-import-low'>Click to get Google Maps navigation</span>`
popup.setLngLat(e.lngLat).setHTML(newHTML).addTo(map);
}
map.on('mouseenter', 'parking', mouseenterparking);
map.on('mouseleave', 'parking', () => { map.getCanvas().style.cursor = ''; popup.remove(); });
map.on('click', 'parking', (e) => { window.open(`https://www.google.com/maps/dir//${e.lngLat.lat},${e.lngLat.lng}`, '_blank', 'noreferrer=true') });
// map.on('mouseenter', 'parking-poly', mouseenterparking);
// map.on('mouseleave', 'parking-poly', () => {
// map.getCanvas().style.cursor = '';
// popup.remove();
// });
// map.on('click', 'parking-poly', (e) => { window.open(`https://www.google.com/maps/dir//${e.lngLat.lat},${e.lngLat.lng}`, '_blank', 'noreferrer=true') });
// Station events
var latestStation = '';
map.on('mouseenter', 'stations', (e) => {
map.getCanvas().style.cursor = 'pointer';
let prop = e.features[0].properties;
latestStation = prop.sterm;
popup.setLngLat(e.lngLat).setHTML(`<b>${prop.name}</b>`).addTo(map);
});
map.on('mouseleave', 'stations', () => { map.getCanvas().style.cursor = ''; popup.remove(); });
map.on('click', 'stations', (e) => {
popup.remove();
let dist = $('input[name="distance"]:checked').val();
$('#select_station').val(`${latestStation},`);
map.flyTo({ center: e.lngLat, zoom: zoom_extents[dist] });
refresh();
});
// map.on('render', () => {
// if ((!zoomed_in && map.getZoom() > 16) || zoomed_in && map.getZoom() < 16) {
// zoomed_in = !zoomed_in;
// $('.parking-poly').toggleClass('hide');
// $('.parking-point').toggleClass('hide');
// }
// });
}
function modalSubmit() {
$('*').css('cursor', 'progress');
// Get new vars from input
let dist = $('input[name="mdl-distance"]:checked').val();
let station = $('#mdl_select_station').val();
let street = $('input[name="mdl-street"]:checked').val();
console.log(station);
// Update sidebar to match
$(`input[name="distance"][value='${dist}']`).prop('checked', true);
$('#select_station').val(station);
$(`input[name='street'][value='${street}']`).prop('checked', true);
// Update map to match
refresh();
// Close the modal
$('*').css('cursor', '');
modal.hide();
}
// On document ready
$(async () => {
// Load page with appropriate styling
dark = await (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches); // Get the users browser dark / light mode preference
await Promise.all([
(async () => {
$('body').addClass(dark ? 'dark-theme' : 'light-theme'); // Set styling variables based on theme
$('#style-switch').html(dark ? 'light_mode' : 'dark_mode'); // Set theme switch symbol
// Update styling when clicked
$('#style-switch').click(() => {
// Indiciate progress
$('*').css('cursor', 'wait');
// Switch styling variables
$('body').toggleClass('dark-theme');
$('body').toggleClass('light-theme');
// Switch symbol
$('#style-switch').html(dark ? 'dark_mode' : 'light_mode')
// Update map styling
map.setStyle(dark ? 'mapbox://styles/seanbrooker/clx3zjjug001n01mwhyf4ex4q' : 'mapbox://styles/seanbrooker/clvxa3q1i01ro01q17rom1b2o')
map.once('styledata', async () => {
await loadData();
await refresh();
$('*').css('cursor', '');
});
dark = !dark
});
})(),
(async () => {
// Init map
mapboxgl.accessToken = accessKey;
map = new mapboxgl.Map({
container: 'map',
style: dark ? 'mapbox://styles/seanbrooker/clvxa3q1i01ro01q17rom1b2o' : 'mapbox://styles/seanbrooker/clx3zjjug001n01mwhyf4ex4q',
maxBounds: [
[144.90259490906936, -37.8380567022436],
[145.00490494442744, -37.76095901624997]
],
pitchWithRotate: false,
bounds: [
[145.00490494442744, -37.76095901624997],
[144.90259490906936, -37.8380567022436]
],
customAttribution: 'Design: Sean Brooker | Data: City of Melbourne, 2024',
performanceMetricsCollection: false,
});
map.on('load', initMap)
})(),
(async () => {
// Init modal
modal = new bootstrap.Modal('#modal', {});
$('#mdl_select_station').val('');
modal.show();
})(),
(async () => {
// Filter update listeners
$('.btn-test').on('change', refresh);
$('#select_station').on('change', refresh);
})()
]).then(() => console.log("Initialised"));
});