From 31fefd5cabb79718bf9e83238da641b84f1998b7 Mon Sep 17 00:00:00 2001 From: Caleb Denio Date: Tue, 21 Oct 2025 22:22:31 -0600 Subject: [PATCH] update in realtime! --- cmd/main.go | 4 ++++ static/index.html | 47 +++++++++++++++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 7c8a93e..7d60ce4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -186,11 +186,15 @@ func feedifyVehicles(vehicles []*pb.VehiclePosition) pb.VehicleFeed { } func main() { + fmt.Println("Opening database...") + _db, err := sql.Open("sqlite3", "uta-gtfs.db") if err != nil { log.Fatal(err) } + fmt.Println("Loading trip data...") + scheduleDb = _db if err := loadTrips(); err != nil { log.Fatalln(err) diff --git a/static/index.html b/static/index.html index fe5c8cf..dfd7e98 100644 --- a/static/index.html +++ b/static/index.html @@ -35,6 +35,7 @@ 4: "#77777a", 5: "#c227b9", }; + let markers = [] var map = L.map("map").setView([40.656734, -111.890818], 12); L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", { @@ -43,24 +44,38 @@ '© OpenStreetMap', }).addTo(map); - protobuf.load("/schema.proto").then(async (root) => { - const bin = await fetch("/api").then((r) => r.arrayBuffer()); - const { vehicles } = root - .lookupType("VehicleFeed") - .decode(new Uint8Array(bin)); + function clearMap() { + for (const marker of markers) { + marker.remove(); + } - vehicles.forEach((vehicle) => { - L.circleMarker([vehicle.lat, vehicle.lon], { - fillColor: COLORS[vehicle.line], - color: COLORS[vehicle.line], - fillOpacity: 0.5, - radius: 8, - }) - .addTo(map) - .bindPopup( - `${vehicle.headsign}
@ ${vehicle.nearestStation.name}` + markers = []; + } + + protobuf.load("/schema.proto").then((root) => { + setInterval(async () => { + const bin = await fetch("/api").then((r) => r.arrayBuffer()); + const { vehicles } = root + .lookupType("VehicleFeed") + .decode(new Uint8Array(bin)); + + clearMap(); + + vehicles.forEach((vehicle) => { + markers.push( + L.circleMarker([vehicle.lat, vehicle.lon], { + fillColor: COLORS[vehicle.line], + color: COLORS[vehicle.line], + fillOpacity: 0.5, + radius: 8, + }) + .addTo(map) + .bindPopup( + `${vehicle.headsign}
@ ${vehicle.nearestStation.name}` + ) ); - }); + }); + }, 5000); });