Bryan Hamilton / writing

bryanhamilton.info / writing / article

I Sort My Ticker by How Far the DJ Is From You

The same 25 DJs, in a different order for every visitor. The bottom ticker on this very site now haversine-sorts the DJ Calendar top 25 by each artist's next show relative to you - and it has a deny path nobody has to crash into.

  • Ships: geolocation prompt on load, distance sort on allow, rank order on deny
  • Data: next-show coordinates for 25 artists, one static same-origin JSON file
  • Server cost per visitor: zero - the sort happens in your browser

A flat list for every human on Earth

My homepage has an audio ticker pinned to the bottom. It scrolls the top 25 artists on my DJ Calendar site by tour miles - the guy who has flown the most between gigs is first. It's a fun stat. It's also a completely flat list: the same order for a visitor in Newark as a visitor in Osaka, every single load.

Today I made it answer a better question. Not "who has traveled the most?" but "who is playing closest to you?"

Same 25 DJs. Different order for every visitor. Here's how it works and where it almost went sideways.

The data was already there

The top 25 comes straight off the DJ Calendar's tour-miles leaderboard. The next-show locations were the missing piece, and it turned out I didn't need to fetch anything new per visitor: the site's own tour-map feed already carries a latitude and longitude for each artist's next appearance. Twenty-five records, a few kilobytes, dumped to a static JSON file on the portfolio side.

Static file matters. The DJ Calendar and the portfolio are different domains, and the feed has no CORS headers - a browser-side fetch straight from bryanhamilton.info to thedjcalendar.com dies instantly. Serving the same data same-origin from a generated sidecar makes the whole problem disappear. When the tour data changes, a refresh job regenerates the file. No proxy, no latency, no third request chain.

Haversine in eleven lines

Distance between two points on a sphere is the haversine formula, and it fits in a tweet. The whole runtime block, appended before </body>:

<script id="bhtickgeo-marker">
(function(){
  fetch("/tickgeo.json",{cache:"no-store"}).then(function(r){return r.json();}).then(function(j){
    function hav(a,b,c,d){
      var R = 3959, t = Math.PI / 180;
      var dLa = (c - a) * t, dLo = (d - b) * t;
      var A = Math.sin(dLa/2) * Math.sin(dLa/2) +
              Math.cos(a*t) * Math.cos(c*t) *
              Math.sin(dLo/2) * Math.sin(dLo/2);
      return R * 2 * Math.atan2(Math.sqrt(A), Math.sqrt(1 - A));
    }
    navigator.geolocation.getCurrentPosition(function(pos){
      var sorted = j.djs.map(function(d){
        d.mi = Math.round(hav(pos.coords.latitude, pos.coords.longitude, d.lat, d.lng));
        return d;
      }).sort(function(a,b){ return a.mi - b.mi; });
      render(sorted);
    }, function(){ render(j.djs); }, {timeout: 4000, maximumAge: 600000});
  });
})();
})();</script>

That's it. Earth-radius in statute miles, so the numbers read naturally to an American tour audience. Five decimal places of latitude times some trig and suddenly the ticker knows Nottingham is closer to Rotterdam than Cairns.

The part that actually matters: the prompt

The sorting is the easy code. The geolocation prompt is the real design decision, because a browser asking "bryanhamilton.info wants to know your location" is a trust moment, and most people's finger goes straight to deny.

So deny is a first-class path, not an error case:

  • Share location: the ticker re-renders as "TOP 25 SHOWS NEAREST YOU," each entry with its distance in miles.
  • Deny or time out: the ticker renders in default rank order with a small label inviting you to share your location for distance sort.

Either way you get a full ticker. Nobody hits an empty bar, a spinner, or a console error. The position call uses a four-second timeout and a ten-minute maximumAge, so a second visit doesn't re-prompt and a slow GPS lock doesn't hang the ticker waiting.

One detail I like: the distances are computed in the browser, per visitor, from the same sidecar file. There's no per-visitor server work at all. The page costs the same to serve whether one person loads it or ten thousand.

What "Live Verified" means here

Every claim in this post is checkable on the live site. Ticker at the bottom of the homepage, prompt on load, re-sort on allow, rank order on deny. The deploy is logged in the public receipts feed, which is itself a JSON file I built so machines - and skeptical readers - can quote my ship history back at me.

The gotchas worth stealing

  • Same-origin sidecar beats cross-domain fetches. If the other site doesn't send CORS headers, stop trying to fight it and generate a copy you control.
  • Fallbacks are the feature. A geolocation feature without a good deny path is just a broken ticker with extra steps.
  • Sort client-side from static data. The per-visitor variance costs nothing server-side.

The ticker will drift as tours announce and shows pass - the sidecar is a snapshot, and the refresh job is the honest maintenance cost. That's fine. A location-aware ticker that's an hour stale is still better than one that's identical for every human on earth.

So: would you hand a website your location for a line of scrolling text? The chat box in the corner is open - I genuinely don't know the answer, which is half the reason I built it.

Subscribe via RSS· every article the moment it ships. Point any reader atbryanhamilton.info/rss.xml

More from the workshop

All writingEvery article, on this domain. Open source11 production-extracted projects.

Every change to this site is logged in the ship log

Every one of these builds is logged in the ship log. Follow along: RSS or The DJ Calendar newsletter.