Maplace.js

Data dashboards

Bubble map

Cities as bubbles sized by population, a legend, and a count of what is in view.

Markdown

Map and code

Run the examples on

Population 30M 10M 3M

Now in view –

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 cities.jsonStyled by bubbles.css

<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- The world's largest cities as bubbles, each as big as its population, the list beside
     it, which "This area" narrows to the map's view, and under the map the legend's sizes and a
     count of what it shows. The cities come from cities.json, largest first so the small bubbles
     are drawn over the big ones; a bubble's area is its population, Tokyo's 37M is 56px across.
     The populations are rounded, for the example. -->
<!-- This example's look lives in the examples' styles/bubbles.css, which every version links. -->
<link rel="stylesheet" href="../../styles/bubbles.css">
<style>
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #bubbles-maplibre-map { height: 36rem; }
    #bubbles-maplibre-map > .maplace-list-view { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<maplace-map id="bubbles-maplibre-map" list="maplibre-bubbles-cities" list-heading="Cities" list-scope="viewport"
  src="/examples/data/cities.json"></maplace-map>
<p class="bubbles-legend">Population
  <span class="bubbles-size"><span class="bubble" aria-hidden="true" style="width: 50.4px"></span>30M</span>
  <span class="bubbles-size"><span class="bubble" aria-hidden="true" style="width: 29.1px"></span>10M</span>
  <span class="bubbles-size"><span class="bubble" aria-hidden="true" style="width: 15.9px"></span>3M</span>
</p>
<p>Now in view <output id="bubbles-maplibre-in-view">–</output></p>
<script>
  // The markers, the list's entries and the count under the map, the page's own: defined as the
  // page is read, so the map finds them when it opens.
  {
    const compact = new Intl.NumberFormat("en", { notation: "compact", maximumFractionDigits: 1 });
    const map = document.getElementById("bubbles-maplibre-map");
    // A city's population, from its data in cities.json.
    const populationOf = ({ data }) => data.population;

    // A city's marker: a circle as big as its population, none under 6px across. Set before the
    // map is made (below, by reading map.maplace), which takes it then.
    map.markerIcon = (city) => {
      const bubble = document.createElement("span");
      bubble.className = "bubble";
      const diameter = Math.max(6, Math.sqrt(populationOf(city) / 37e6) * 56);
      bubble.style.width = bubble.style.height = `${diameter}px`;
      return bubble;
    };

    // A city in the list: its name, then its population.
    const cityEntry = ({ location }) => {
      const entry = document.createElement("span");
      entry.className = "bubbles-city";
      const population = document.createElement("span");
      population.className = "bubbles-population";
      population.textContent = compact.format(populationOf(location));
      entry.append(location.title, population);
      return entry;
    };
    Maplace.defineListView("maplibre-bubbles-cities", (maplace, { options }) =>
      Maplace.createListView(maplace, { ...options, render: cityEntry }));

    // Whether a city is inside the bounds, which may cross the antimeridian (west > east).
    const inView = ({ lat, lng }, { north, south, east, west }) => {
      const inLongitude = west <= east ? lng >= west && lng <= east : lng >= west || lng <= east;
      return lat >= south && lat <= north && inLongitude;
    };
    // How many cities, and how many people in them: "12 cities · 210M people".
    const summaryOf = (cities) => {
      const people = cities.reduce((sum, city) => sum + populationOf(city), 0);
      return `${cities.length} cities · ${compact.format(people)} people`;
    };
    // What the map shows, after each move, its first included.
    const inViewNow = document.getElementById("bubbles-maplibre-in-view");
    map.maplace.on("viewportChange", ({ viewport }) => {
      const { locations } = map.maplace.getSnapshot();
      if (viewport.bounds) inViewNow.value = summaryOf(locations.filter((city) => inView(city, viewport.bounds)));
    });
  }
</script>

How it works

The world’s largest cities as bubbles, each as big as its population, the list beside the map, which “This area” narrows to the map’s view, and under the map the legend’s sizes and a count of what it shows. The populations are rounded and approximate, for the example.

  • HTML: src names cities.json (Data), and the page’s script sets markerIcon, which sizes each city’s bubble from its data.population (Properties and methods); list-scope="viewport" puts the “All” and “This area” switch above the list (Map attributes), a list view of the page’s own reads each population from the location’s data (List views), and a viewportChange listener counts what is in view.
  • Script and Bundler: the cities come from cities.json; each bubble is the city’s icon, a circle sized in pixels against the largest city’s population, so the legend’s sizes hold at every zoom (Icons); list: { scope: "viewport", render } (List view options), and a viewportChange listener counts the cities in the map’s bounds (Events).
  • React: markerIcon on <MaplaceMap>, <LocationList scope="viewport">, and onViewportChange on <MaplaceProvider> into state (Event props).
  • Note: the cities are listed largest first, and a marker added later is drawn over one before it, so the small bubbles stay on top and can be clicked.