Maplace.js

A list beside the map,
on Google Maps
and MapLibre.

Your locations as a list kept in sync with the map: a select menu, tag filters, nested groups with a checkbox each, a search box, nearest first, only what's in view, your own markup, or tabs with a details panel.

GitHub

Google Maps

<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<maplace-map api-key="YOUR_KEY" list>
  <maplace-location location-id="rome" lat="41.9028" lng="12.4964" title="Rome"></maplace-location>
  <maplace-location location-id="milan" lat="45.4642" lng="9.19" title="Milan"></maplace-location>
</maplace-map>

MapLibre, no key

<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<maplace-map list>
  <maplace-location location-id="rome" lat="41.9028" lng="12.4964" title="Rome"></maplace-location>
  <maplace-location location-id="milan" lat="45.4642" lng="9.19" title="Milan"></maplace-location>
</maplace-map>

Install

npm and CDN script

Google Maps

npm install @maplacejs/maplace

MapLibre

npm install @maplacejs/maplace maplibre-gl

CDN script

Google Maps

<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1.2.0/dist/maplace-element.iife.js"
  integrity="sha384-OgwZw1NQ78VKxQUXntJyiiRILF/Ck2chxPi2kbY+gNOcSqZIIsBVXI1BAPbqs6LV" crossorigin="anonymous"></script>

MapLibre

<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1.2.0/dist/maplace-element-maplibre.iife.js"
  integrity="sha384-NTx1K+93ZQC7ezi/smTAvSpiQCrdWid6IfMLQuIS6oT/63JARr9b+gD1devKzZqV" crossorigin="anonymous"></script>

Maplace.js 1.2.0, checked by the browser against its hash.

Run the examples on

The list beside the map, your way: a menu, tags, nested groups, a search box, nearest first, what's in view, your own markup, or tabs.

Markdown

06 Filter by tags

Places with several tags: tick some to see those with all of them.

Run the map from

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

A location has one group, its heading in the list, and as many tags as it needs. Tick a box and the map and the list keep the places with all the ticked tags; untick them all and every place is back. The boxes sit above the map, the list on it, top right.

  • Try it: tick “Parking”, then “Open late” too: the numbers after the headings follow what is listed.
  • HTML: tags="parking open-late" on <maplace-location>, the tags split on spaces (Location attributes), group-counts on <maplace-map> for the numbers (Map attributes), and a script calling the element’s maplace.setFilter.
  • Script and Bundler: tags: ["parking", "open-late"] on a location (Every location), groupCounts on the list (List view options), and setFilter({ tags }) with what is ticked: no tags ask for nothing, so no branch is needed for none.
  • React: <LocationList groupCounts /> and the filter prop of <MaplaceProvider>, filter={{ tags }}, applied when its value changes.
  • With groups: setFilter({ groups: ["North"], tags: ["parking"] }) keeps what matches both; setFilter(["North"]), a list of groups alone, is what the Select menu example does.

07 Filter by group

Offices by region and country, nested in the list, with a checkbox for each region and country.

Fetches offices.json

Run the map from

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

A company’s offices around the world, each in a region and a country: its group is a path, outermost first, so the list nests the countries under their regions, each heading counting everything under it, and a checkbox for each region and country above the list filters the map and the list.

  • Try it: open Groups above the list and untick Italy: Europe’s box is partly ticked, its count drops by Italy’s offices, and Italy’s pins leave the map. Click Europe: all of Europe is back. Untick every box and nothing is listed; tick them all and the filter asks for no group at all. Type in the search box while some boxes are unticked: the boxes set only the filter’s groups, the box only its words, so each keeps what the other asked for.
  • A partly ticked group: it shows its ticked subgroups, not entries of its own (those whose path ends at it, such as group: ["Europe"]): a group in the filter always takes all its subgroups. This map has none at a region.
  • HTML: group='["Europe","Italy"]' on <maplace-location>, the path as JSON: a group starting with [ is read as a path (Location attributes); group-filter, group-counts and search on <maplace-map> (Map attributes).
  • Script and Bundler: group: ["Europe", "Italy"] on each office, up to 3 names (Every location), and groupFilter, groupCounts and search on the list (List view options). The boxes call setFilter for you: setFilter([["Europe", "Italy"]]) asks for one path, Italy’s offices, while setFilter(["Europe", "Italy"]) asks for two groups, Europe and a top-level Italy; setFilter(["Europe"]) shows Europe’s countries too.
  • React: <LocationList groupFilter groupCounts search /> inside <MaplaceProvider> (<LocationList>).

09 Nearest first

A button on the map that sorts the list by distance from the visitor.

Run the map from

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

The My location button on the map asks the browser for the visitor’s position only when it is pressed, then sorts the list nearest first, each entry saying how far in a straight line, marks the place with a dot and moves the map there.

  • Try it: press My location, top left on the map, and allow it; press it again and the list goes back. If the visitor refuses, or the page isn’t on HTTPS, a line under the button says “Location unavailable” and nothing else changes.
  • HTML: list-sort="distance" and my-location on <maplace-map>, with distance-unit="mile" for miles (Map attributes).
  • Script and Bundler: sort: "distance" in list, and myLocation: true in the map’s options (Distances).
  • React: <LocationList sort="distance" />, and <MyLocation /> inside the <MaplaceMap> (<MyLocation>).
  • Another point: setOrigin takes any point, so a click measures from there: map.on("mapClick", ({ position }) => map.setOrigin(position)), and a form of your own measures from a typed address.

10 This area

A switch above the list: every place, or only those in view as the map moves.

Run the map from

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

The list on the map, top left, has a switch above it: every place, or only those in view as the map moves.

  • Try it: “All”, pressed at first, lists every capital. Press “This area”, and the list holds those in view, listing again as you pan or zoom; choosing an entry moves the map too. Over an empty patch of sea, the list says so. “All” lists them all again.
  • HTML: list-scope="viewport" on <maplace-map>, beside list and list-position, with area-only-label, show-all-label and empty-area-label for its wording (Map attributes).
  • Script and Bundler: list: { scope: "viewport", position: "top-left" } in the map’s options, with areaOnlyLabel, showAllLabel and emptyAreaLabel (List view options; position, on Google, on MapLibre).
  • React: <LocationList scope="viewport" position="top-left" /> inside the <MaplaceMap> (<LocationList>).
  • Its corner: any of the four, top-left here. Left of the map is where a list has the most room on Google, which stacks its fullscreen button, camera control and Pegman on the right (On-map controls). Without a position, the list goes top right.

11 Your own list

Define your own: here a radio per place, and one to fit all, on the map.

Run the map from

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

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.

  • Try it: choose a radio and the map goes to that place; choose “Fit all” and it shows them all. Cambridge is on the map only, left out with listed="false".
  • 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>.

12 Tabs

The list outside the map; a details panel follows the selection.

Run the map from

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

The list leaves the map and becomes a row of tabs above it, with a panel under them that follows the selection.

  • Try it: choose a tab and the map goes there, the panel saying a line about it; picking a pin does the same to the tabs. It starts on London.
  • HTML: list and selected-id on <maplace-map> (Map attributes), the page’s CSS drawing the list as tabs through its role="listbox" and role="option", and a maplace-selectionchange listener filling the panel (Events).
  • Script and Bundler: selectedId in the map’s options and list: { container } to mount the list on a node of yours (Options, List view options), and a selectionChange listener (Events).
  • React: defaultSelectedId on <MaplaceProvider>, <LocationList /> outside the <MaplaceMap>, and a component reading the selection with useMaplace().