Maplace.js

Styling

A team around the world

Faces for markers, list entries of your own with each person's local time, filtered by team.

Markdown

Map and code

Run the examples on

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

These maps come from Google Maps and OpenFreeMap, which see your IP address. They stay off until you say so, once for the whole site. What loading the maps sends

Fetches team.jsonStyled by team.css

<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- A team around the world, from team.json: each person's face on the map, their initials on
     their team's colour, and the list beside it, each entry their role and city and the time
     where they are, which turns as each minute begins; filtered by team, or searched by name,
     city or role. On MapLibre, the earth as a globe, with a toggle on the map to flatten it. -->
<!-- This example's look lives in the examples' styles/team.css, which every version links. -->
<link rel="stylesheet" href="../../styles/team.css">
<style>
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #team-maplibre-map { height: 36rem; }
    #team-maplibre-map > .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<maplace-map id="team-maplibre-map" list="maplibre-team-members" list-heading="The team" group-counts
  group-filter search search-label="Find someone" globe
  src="/examples/data/team.json"></maplace-map>
<script>
  {
    const map = document.getElementById("team-maplibre-map");
    const viewerZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    // How long until the next minute begins, and a little past it, so the clock reads the new one.
    const untilNextMinute = () => 60_050 - (Date.now() % 60_000);

    // The time on a zone's clock: "17:30".
    const localTimeOf = (timeZone, now) =>
      new Intl.DateTimeFormat("en-GB", { timeZone, hour: "2-digit", minute: "2-digit", hourCycle: "h23" }).format(now);
    // How far a zone's clock is ahead of UTC, in minutes: Kolkata's 330 ("GMT" alone is 0).
    const minutesFromUtc = (timeZone, now) => {
      const name = new Intl.DateTimeFormat("en-GB", { timeZone, timeZoneName: "longOffset" })
        .formatToParts(now).find(({ type }) => type === "timeZoneName")?.value ?? "";
      const [, sign = "+", hours = "0", minutes = "0"] = /GMT([+\-−])(\d\d):(\d\d)/.exec(name) ?? [];
      return (sign === "+" ? 1 : -1) * (Number(hours) * 60 + Number(minutes));
    };
    // "+5h30", "−8h" or "same time": how far a zone's clock is ahead of yours, or behind.
    const offsetFromYou = (timeZone, now) => {
      const minutes = minutesFromUtc(timeZone, now) - minutesFromUtc(viewerZone, now);
      if (minutes === 0) return "same time";
      const sign = minutes > 0 ? "+" : "−";
      const hours = Math.floor(Math.abs(minutes) / 60);
      const rest = Math.abs(minutes) % 60;
      return `${sign}${hours}h${rest ? String(rest).padStart(2, "0") : ""}`;
    };
    const writeLocalTime = (time) => {
      const now = new Date();
      time.textContent = `${localTimeOf(time.dataset.timeZone, now)} · ${offsetFromYou(time.dataset.timeZone, now)}`;
    };
    // A span showing the time on a zone's clock beside how far it is from yours.
    const localTime = (timeZone) => {
      const time = document.createElement("span");
      time.className = "team-member-time";
      time.dataset.timeZone = timeZone;
      writeLocalTime(time);
      return time;
    };
    const span = (className, ...children) => {
      const element = document.createElement("span");
      element.className = className;
      element.append(...children);
      return element;
    };

    // A person's marker, and their face in the list: their initials on their team's colour.
    const avatarOf = ({ group, data }) => {
      const avatar = span("team-avatar", data.initials);
      avatar.dataset.team = group;
      return avatar;
    };
    map.markerIcon = avatarOf;

    // A person's entry in the list, the page's own: their face, name, role and city from their
    // data in team.json, and the time where they are.
    const memberItem = ({ location }) => {
      const person = location.data;
      const face = avatarOf(location);
      face.setAttribute("aria-hidden", "true");
      return span("team-member", face, span("team-member-text",
        span("team-member-name", location.title),
        span("team-member-detail", `${person.role} · ${person.city}`),
        localTime(person.timeZone)));
    };
    Maplace.defineListView("maplibre-team-members", (maplace, { options }) =>
      Maplace.createListView(maplace, { ...options, render: memberItem }));

    // Every time shown turns as each minute begins.
    const showLocalTimes = () => {
      for (const time of map.querySelectorAll(".team-member-time")) writeLocalTime(time);
      setTimeout(showLocalTimes, untilNextMinute());
    };
    setTimeout(showLocalTimes, untilNextMinute());
  }
</script>

How it works

Each person is a face on the map, their initials on their team’s colour, and the list beside it is the page’s own: their name, role and city, and the time where they are next to how far that is from yours, turning as each minute begins.

  • HTML: src names team.json, the same people the other versions load (Data), and the page’s script sets markerIcon, which draws each person’s face from their data.initials and their group (Properties and methods). The list is a view of the page’s own, Maplace.defineListView around Maplace.createListView with render, which reads the role, city and time zone from location.data and draws the face the same way (List views); group-filter, group-counts and search on the map still reach it as its options.
  • Script and Bundler: the people from team.json, each given an icon that draws the face from its data (Icons), and list.render building each entry (List view options). The times are written into each entry’s own span, and a timeout set to the next minute writes them again, so no location changes for the clock.
  • React: markerIcon on <MaplaceMap> and the entry as <LocationList>’s children (<LocationList>); each entry’s <LocalTime> keeps the minute in its state, so only the times render again as each minute begins.
  • Styling: each team’s colour, and the rings a face takes while pointed at or picked ([data-hovered], [data-selected]), in the examples’ styles/team.css (Styling).
  • On MapLibre, a globe: globe on the map (globe: true in the options; in React, createAdapter={() => createMapLibreAdapter({ globe: true })}) (Globe). Google’s map has no globe.