diff --git a/client/MhdMapClient.js b/client/MhdMapClient.js index c856f35..8134d30 100644 --- a/client/MhdMapClient.js +++ b/client/MhdMapClient.js @@ -68,11 +68,13 @@ export default class MhdMapClient { this.baseMapLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap', - maxZoom: 19 + maxZoom: this.map.options.maxZoom, + maxNativeZoom: 19 }).addTo(this.map); this.georefLayer = L.tileLayer('https://maps.georeferencer.com/georeferences/e886f898-04e0-4479-8cd3-bbe4ae7537b3/2022-06-22T09:24:26.012877Z/map/{z}/{x}/{y}.png?key=c0WCCJEkXmpgh7TxHg2U', { - maxZoom: 17 + maxZoom: this.map.options.maxZoom, + maxNativeZoom: 17, }); this.layerControl = L.control.layers({ @@ -277,6 +279,16 @@ export default class MhdMapClient { } + async fillStopInfo(stop, event) { + let stopData = await this.sendWs('requestStopInfo', { + stationStopId: stop.stationStopId, + }); + if (stopData.data.departures.length === 0) + return; + + console.log(stopData.data.departures.length); + } + async drawVehicleTrace(vehId) { this.currentVehicleTrace.clearLayers(); if (vehId === null) @@ -414,6 +426,12 @@ export default class MhdMapClient { this.drawVehicleTrace(veh.vehicleNumber); } + let stop = event?.popup?._source?.options?.data?.stop; + if (stop) { + // the user has clicked a stop marker + this.fillStopInfo(stop, event); + } + } _popupCloseEvent(event) { diff --git a/server/BratislavaOpendata.js b/server/BratislavaOpendata.js index 62f4650..0352cc2 100644 --- a/server/BratislavaOpendata.js +++ b/server/BratislavaOpendata.js @@ -6,8 +6,8 @@ export default class BratislavaOpendata { this.apiKey = apiKey; } - async fetchOneStop(stationId) { - return this.request('/stationstop/' + stationId); + async fetchOneStop(stationStopId) { + return this.request('/stationstop/' + stationStopId); } async fetchAllStops() { diff --git a/server/MhdMapApp.js b/server/MhdMapApp.js index 1839ef8..25d2ba1 100644 --- a/server/MhdMapApp.js +++ b/server/MhdMapApp.js @@ -14,6 +14,7 @@ export default class MhdMapApp { this.stops = null; this.oldVehicles = null; this.vehicles = null; + this.stopCache = {}; this.lastUpdatedStatic = null; this.lastUpdatedDynamic = null; this.lastDelta = null; @@ -92,6 +93,22 @@ export default class MhdMapApp { } } + async getStopInfo(stationStopId) { + let cached = this.stopCache[stationStopId] || null; + if (cached && cached.t.getTime() > (Date.now() - 15 * 1000)) { // cache for 15 seconds + return cached; + } + + const data = await this.opendata.fetchOneStop(stationStopId); + if (!data) + return cached; // null or stale + + return (this.stopCache[stationStopId] = { + t: new Date(), + data: data, + }); + } + calculateVehiclesDelta(beforeObj, afterObj) { let delta = {}; @@ -216,6 +233,8 @@ export default class MhdMapApp { return await this.recorder.getVehicleTrace(data.vehicle, data.count || 20); } else if (action === 'requestLineTrace') { return await this.recorder.getSimpleLineTrace(data.line, data.count || 20); + } else if (action === 'requestStopInfo') { + return await this.getStopInfo(data.stationStopId); } return null; }