Maplace.js

Lines and routes

Flight paths

Great circles from one city to eight, the scheduled ones dashed; slide the flight to New York along its way.

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

<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- London's flights out, each along its great circle, the shortest way on the earth, from
     the hub to a city, listed beside the map, on a globe its toggle flattens, the cities and the plane on the map only.
     The ones still scheduled are dashed; the one to New York is in the air, active, travelled as
     far as the slider says, its plane there. -->
<style>
  #flights-maplibre-flown { display: flex; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; }
</style>
<label id="flights-maplibre-flown">London → New York, flown
  <input id="flights-maplibre-flown-input" type="range" min="0" max="1" step="0.01" value="0.4">
</label>
<maplace-map id="flights-maplibre-map" list list-heading="Flights from London" globe>
  <script type="application/json">
    [{ "id": "london", "lat": 51.5074, "lng": -0.1278, "title": "London", "listed": false },
     { "id": "new-york", "lat": 40.7128, "lng": -74.006, "title": "New York", "listed": false },
     { "id": "sao-paulo", "lat": -23.5505, "lng": -46.6333, "title": "São Paulo", "listed": false },
     { "id": "cape-town", "lat": -33.9249, "lng": 18.4241, "title": "Cape Town", "listed": false },
     { "id": "dubai", "lat": 25.2048, "lng": 55.2708, "title": "Dubai", "listed": false },
     { "id": "mumbai", "lat": 19.076, "lng": 72.8777, "title": "Mumbai", "listed": false },
     { "id": "singapore", "lat": 1.3521, "lng": 103.8198, "title": "Singapore", "listed": false },
     { "id": "tokyo", "lat": 35.6895, "lng": 139.6917, "title": "Tokyo", "listed": false },
     { "id": "sydney", "lat": -33.8688, "lng": 151.2093, "title": "Sydney", "listed": false },
     { "id": "london-new-york", "type": "polyline", "title": "London → New York", "geodesic": true, "active": true, "progress": 0.4,
       "path": [{ "lat": 51.5074, "lng": -0.1278 }, { "lat": 40.7128, "lng": -74.006 }],
       "style": { "stroke": "#2563eb" } },
     { "id": "london-sao-paulo", "type": "polyline", "title": "London → São Paulo", "geodesic": true,
       "path": [{ "lat": 51.5074, "lng": -0.1278 }, { "lat": -23.5505, "lng": -46.6333 }],
       "style": { "stroke": "#2563eb" } },
     { "id": "london-cape-town", "type": "polyline", "title": "London → Cape Town", "geodesic": true,
       "path": [{ "lat": 51.5074, "lng": -0.1278 }, { "lat": -33.9249, "lng": 18.4241 }],
       "style": { "stroke": "#2563eb", "strokeDash": "dashed" } },
     { "id": "london-dubai", "type": "polyline", "title": "London → Dubai", "geodesic": true,
       "path": [{ "lat": 51.5074, "lng": -0.1278 }, { "lat": 25.2048, "lng": 55.2708 }],
       "style": { "stroke": "#2563eb" } },
     { "id": "london-mumbai", "type": "polyline", "title": "London → Mumbai", "geodesic": true,
       "path": [{ "lat": 51.5074, "lng": -0.1278 }, { "lat": 19.076, "lng": 72.8777 }],
       "style": { "stroke": "#2563eb" } },
     { "id": "london-singapore", "type": "polyline", "title": "London → Singapore", "geodesic": true,
       "path": [{ "lat": 51.5074, "lng": -0.1278 }, { "lat": 1.3521, "lng": 103.8198 }],
       "style": { "stroke": "#2563eb" } },
     { "id": "london-tokyo", "type": "polyline", "title": "London → Tokyo", "geodesic": true,
       "path": [{ "lat": 51.5074, "lng": -0.1278 }, { "lat": 35.6895, "lng": 139.6917 }],
       "style": { "stroke": "#2563eb" } },
     { "id": "london-sydney", "type": "polyline", "title": "London → Sydney", "geodesic": true,
       "path": [{ "lat": 51.5074, "lng": -0.1278 }, { "lat": -33.8688, "lng": 151.2093 }],
       "style": { "stroke": "#2563eb", "strokeDash": "dashed" } }]
  </script>
</maplace-map>
<script type="module">
  // The slider flies the flight to New York: its progress, and the plane where that puts it.
  const map = document.getElementById("flights-maplibre-map");
  const flown = document.getElementById("flights-maplibre-flown-input");
  const plane = { id: "plane", title: "The plane to New York", listed: false,
    icon: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='28' height='28' viewBox='0 0 24 24'%3E%3Cpath fill='%231d4ed8' stroke='white' d='M12 2l1.5 7 8.5 4v2l-8.5-2.5-.5 5.5 2.5 2v1.5l-3.5-1-3.5 1V20l2.5-2-.5-5.5L2 15v-2l8.5-4z'/%3E%3C/svg%3E" };
  // The flights reach the map as it opens, after this script runs.
  await map.maplace.whenReady();
  const locations = map.locations;
  function fly() {
    const progress = flown.valueAsNumber;
    const current = locations.map((location) =>
      location.id === "london-new-york" ? { ...location, progress } : location,
    );
    const inTheAir = current.find(({ id }) => id === "london-new-york");
    map.locations = [...current, { ...plane, ...Maplace.pointAlong(inTheAir, progress) }];
  }
  fly();
  flown.addEventListener("input", fly);
</script>

How it works

London’s flights out, each along its great circle, the shortest way on the earth: a polyline of two points with geodesic: true, listed beside the map, while the cities and the plane are on the map only. The flights still scheduled are dashed, and the one to New York is in the air: drawn active, travelled as far as the slider says, with its plane there.

  • HTML: a <script type="application/json"> inside <maplace-map> holding the data, as a path takes more than an attribute (Data); list and list-heading on the map (Map attributes); and a few lines of script for the slider, once the map is ready (maplace.whenReady()), setting the flight’s progress and the plane at Maplace.pointAlong through the element’s locations property (Properties and methods).
  • The looks: strokeDash: "dashed" in a flight’s style (Styles); active: true and progress on the flight in the air, its travelled part faded unless travelledStyle says otherwise (Polylines); the plane a marker at pointAlong(flight, progress) (A point along a line), moved with every new set of locations.
  • Script and Bundler: type: "polyline", path and geodesic on each flight (Curves and great circles), listed: false on each city (Common fields), and list: { container, heading } (List view options).
  • React: the same locations on <MaplaceProvider>, and <LocationList heading="Flights from London" /> beside the <MaplaceMap> (<LocationList>).
  • On MapLibre, a globe: globe on the map (globe: true in the options; in React, createAdapter={() => createMapLibreAdapter({ globe: true })}) opens it as the earth, with a “Globe” toggle that flattens it (Globe). The globe shows London’s half: turn it, or flatten it, to see every flight. Google’s map has no globe.