Back

Case study: a live map of ~1,800 Singapore buses

Cover image for Case study: a live map of ~1,800 Singapore buses

Live demo: harryakbar.dev/transit

The problem

Singapore's LTA DataMall publishes bus positions, but not as one feed. You ask a single bus stop which buses are coming, and each answer includes where those buses are right now. The API key must also stay secret, so the browser can't call DataMall directly.

So the question was: how do you see as many buses as possible, with as few requests as possible, without leaking the key?

What I built

  • A small API on Cloudflare Workers (Hono). The browser calls the Worker, and the Worker calls DataMall's BusArrival API with the key from a secret. It accepts up to 30 stops per request, drops empty or out-of-Singapore positions, removes duplicate buses, and caches each answer for 20 seconds.
  • Smarter stop selection. Instead of picking busy interchanges, I scanned DataMall and chose 150 stops greedily: each stop is the one that adds the most buses not already seen. Those 150 stops show ~1,800 buses, against ~800 for the best 30.
  • Parallel polling. The page polls the 150 stops in parallel batches of 30 every 20 seconds, then removes buses reported by more than one stop.
  • A map that stays smooth. Buses move smoothly between updates on a MapLibre map. Tap a bus to see its service number. If the API fails on first load, the page falls back to simulated buses, and a WebGPU compute shader can drive 50,000+ simulated vehicles.

Result

  • ~1,800 buses on the map from 150 chosen stops. The best 30 stops alone give ~800.
  • The API key never reaches the browser.
  • Light load on DataMall: each stop set is cached for 20 s at the edge and shared by all visitors.

Stack

Next.js · TypeScript · MapLibre GL · WebGPU · Hono · Cloudflare Workers · LTA DataMall