Maplace.js

Lists and menus

Custom list

A list of your own markup: here a radio per place, and one to fit all, on the map.

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>
<!-- A list view of your own: a radio per listed location and one to fit them all, defined by
     name and placed on the map as Maplace.js's own lists are. -->
<style>
  #custom-list-maplibre-map fieldset { border: 0; padding: 0; }
  #custom-list-maplibre-map fieldset label { display: inline-block; margin-right: 0.75rem; }
</style>
<maplace-map id="custom-list-maplibre-map" list="maplibre-checks" list-position="top-right"
  list-heading="Choose a location:">
  <maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London"></maplace-location>
  <maplace-location location-id="york" lat="53.9591" lng="-1.0815" title="York"></maplace-location>
  <maplace-location location-id="bath" lat="51.3811" lng="-2.359" title="Bath"></maplace-location>
  <maplace-location location-id="oxford" lat="51.752" lng="-1.2577" title="Oxford"></maplace-location>
  <maplace-location location-id="cambridge" lat="52.2053" lng="0.1218" title="Cambridge" listed="false"></maplace-location>
</maplace-map>
<script>
  Maplace.defineListView('maplibre-checks', (maplace, { options, listed }) => {
    const fieldset = document.createElement('fieldset');
    const legend = document.createElement('legend');
    legend.textContent = options.heading ?? 'Locations';
    const choices = document.createElement('div');
    fieldset.append(legend, choices);

    /** A radio choosing `id`, or fitting every location when `id` is empty. */
    function choice(id, title) {
      const radio = document.createElement('input');
      radio.type = 'radio';
      radio.name = 'custom-list-maplibre-choice';
      radio.value = id;
      radio.addEventListener('change', () => {
        maplace.select(id || undefined);
        if (!id) maplace.fitAll();
      });
      const label = document.createElement('label');
      label.append(radio, ` ${title}`);
      return label;
    }

    // The choices are made again when the listed locations change; the selection is checked.
    let listedIds;
    function render() {
      const locations = listed();
      const ids = locations.map(({ id }) => id).join();
      if (ids !== listedIds) {
        listedIds = ids;
        choices.replaceChildren(choice('', 'Fit all'), ...locations.map(({ id, title }) => choice(id, title ?? id)));
      }
      const selected = maplace.getSnapshot().selectedId ?? '';
      for (const radio of choices.querySelectorAll('input')) radio.checked = radio.value === selected;
    }

    render();
    const unsubscribe = maplace.subscribe(render);
    return { element: fieldset, destroy: () => { unsubscribe(); fieldset.remove(); } };
  });
</script>

How it works

Maplace.js’s lists are views of its store, and yours can be any element you build: one that calls the store’s select and fitAll, and redraws when the store changes. This one is a radio per place and one to fit them all; each version below shows a way to put it on the map, and it can sit beside the map as any list can.

  • HTML: define it by name with defineListView(name, factory), Maplace.defineListView on the CDN script, and name it in list: checks here, maplibre-checks on the MapLibre page, since one document defines a name once. The factory gets the store and listed(), the locations Maplace.js’s own lists would show, and returns { element, destroy }; list-position places it as it does the built-in lists.
  • Script: the Bundler version’s code on the CDN script’s Maplace, with no build step: Maplace.createMaplace, Maplace.listedLocations and the adapter’s mountControl, as below.
  • Bundler: redraw the element from listedLocations(snapshot) whenever the store’s subscribe calls back, and put it on the map with the adapter’s mountControl(node, position).
  • React: a component reading the store with useMaplace() and useSyncExternalStore, inside a <MapControl>.