Custom list
A list of your own markup: here a radio per place, and one to fit all, on the map.
Map and code
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><!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.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-map fieldset { border: 0; padding: 0; }
#custom-list-map fieldset label { display: inline-block; margin-right: 0.75rem; }
</style>
<maplace-map id="custom-list-map" api-key="YOUR_KEY" list="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('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-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><!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet, what the map mounts on, and the map made with Maplace's store and MapLibre adapter, with a list of your own on it. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
#custom-list-maplibre-script-map { height: 24rem; }
#custom-list-maplibre-script-map fieldset { border: 0; padding: 0; }
#custom-list-maplibre-script-map fieldset label { display: inline-block; margin-right: 0.75rem; }
</style>
<div id="custom-list-maplibre-script-map"></div>
<script type="module">
const locations = [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "york", lat: 53.9591, lng: -1.0815, title: "York" },
{ id: "bath", lat: 51.3811, lng: -2.359, title: "Bath" },
{ id: "oxford", lat: 51.752, lng: -1.2577, title: "Oxford" },
{ id: "cambridge", lat: 52.2053, lng: 0.1218, title: "Cambridge", listed: false },
];
/** A radio choosing `id`, or fitting every location when `id` is empty. */
function choice(maplace, id, title) {
const radio = document.createElement("input");
radio.type = "radio";
radio.name = "custom-list-maplibre-script-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;
}
/**
* A list of your own: a radio per location Maplace.js's lists would list, and one to fit them all.
* The choices are made again when those change; the selection is checked.
*/
function checksMenu(maplace, heading) {
const fieldset = document.createElement("fieldset");
const legend = document.createElement("legend");
legend.textContent = heading;
const choices = document.createElement("div");
fieldset.append(legend, choices);
let listedIds;
const render = () => {
const listed = Maplace.listedLocations(maplace.getSnapshot());
const ids = listed.map(({ id }) => id).join();
if (ids !== listedIds) {
listedIds = ids;
const entries = listed.map(({ id, title }) => choice(maplace, id, title ?? id));
choices.replaceChildren(choice(maplace, "", "Fit all"), ...entries);
}
const selected = maplace.getSnapshot().selectedId ?? "";
for (const radio of choices.querySelectorAll("input"))
radio.checked = radio.value === selected;
};
render();
maplace.subscribe(render);
return fieldset;
}
const maplace = Maplace.createMaplace({ locations });
const adapter = Maplace.createMapLibreAdapter();
maplace.attach(adapter, "#custom-list-maplibre-script-map");
adapter.mountControl(checksMenu(maplace, "Choose a location:"), "top-right");
</script><!doctype html>
<!-- A page of your own script: the CDN script, what the map mounts on, and the map made with Maplace's store and Google adapter, which adds Maps JS with your key, with a list of your own on it. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
#custom-list-script-map { height: 24rem; }
#custom-list-script-map fieldset { border: 0; padding: 0; }
#custom-list-script-map fieldset label { display: inline-block; margin-right: 0.75rem; }
</style>
<div id="custom-list-script-map"></div>
<script type="module">
const locations = [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "york", lat: 53.9591, lng: -1.0815, title: "York" },
{ id: "bath", lat: 51.3811, lng: -2.359, title: "Bath" },
{ id: "oxford", lat: 51.752, lng: -1.2577, title: "Oxford" },
{ id: "cambridge", lat: 52.2053, lng: 0.1218, title: "Cambridge", listed: false },
];
/** A radio choosing `id`, or fitting every location when `id` is empty. */
function choice(maplace, id, title) {
const radio = document.createElement("input");
radio.type = "radio";
radio.name = "custom-list-script-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;
}
/**
* A list of your own: a radio per location Maplace.js's lists would list, and one to fit them all.
* The choices are made again when those change; the selection is checked.
*/
function checksMenu(maplace, heading) {
const fieldset = document.createElement("fieldset");
const legend = document.createElement("legend");
legend.textContent = heading;
const choices = document.createElement("div");
fieldset.append(legend, choices);
let listedIds;
const render = () => {
const listed = Maplace.listedLocations(maplace.getSnapshot());
const ids = listed.map(({ id }) => id).join();
if (ids !== listedIds) {
listedIds = ids;
const entries = listed.map(({ id, title }) => choice(maplace, id, title ?? id));
choices.replaceChildren(choice(maplace, "", "Fit all"), ...entries);
}
const selected = maplace.getSnapshot().selectedId ?? "";
for (const radio of choices.querySelectorAll("input"))
radio.checked = radio.value === selected;
};
render();
maplace.subscribe(render);
return fieldset;
}
const maplace = Maplace.createMaplace({ locations });
const adapter = Maplace.createGoogleAdapter({ apiKey: "YOUR_KEY" });
maplace.attach(adapter, "#custom-list-script-map");
adapter.mountControl(checksMenu(maplace, "Choose a location:"), "top-right");
</script><!doctype html>
<!-- The page vanilla.ts runs on: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#custom-list-maplibre-vanilla-map { height: 24rem; }
#custom-list-maplibre-vanilla-map fieldset { border: 0; padding: 0; }
#custom-list-maplibre-vanilla-map fieldset label { display: inline-block; margin-right: 0.75rem; }
</style>
<div id="custom-list-maplibre-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>import "maplibre-gl/dist/maplibre-gl.css";
import { createMaplace, type Location, listedLocations, type Maplace } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
const locations: Location[] = [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "york", lat: 53.9591, lng: -1.0815, title: "York" },
{ id: "bath", lat: 51.3811, lng: -2.359, title: "Bath" },
{ id: "oxford", lat: 51.752, lng: -1.2577, title: "Oxford" },
{ id: "cambridge", lat: 52.2053, lng: 0.1218, title: "Cambridge", listed: false },
];
/** A radio choosing `id`, or fitting every location when `id` is empty. */
function choice(maplace: Maplace, id: string, title: string): HTMLLabelElement {
const radio = document.createElement("input");
radio.type = "radio";
radio.name = "custom-list-maplibre-vanilla-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;
}
/**
* A list of your own: a radio per location Maplace.js's lists would list, and one to fit them all.
* The choices are made again when those change; the selection is checked.
*/
function checksMenu(maplace: Maplace, heading: string): HTMLFieldSetElement {
const fieldset = document.createElement("fieldset");
const legend = document.createElement("legend");
legend.textContent = heading;
const choices = document.createElement("div");
fieldset.append(legend, choices);
let listedIds: string | undefined;
const render = () => {
const listed = listedLocations(maplace.getSnapshot());
const ids = listed.map(({ id }) => id).join();
if (ids !== listedIds) {
listedIds = ids;
const entries = listed.map(({ id, title }) => choice(maplace, id, title ?? id));
choices.replaceChildren(choice(maplace, "", "Fit all"), ...entries);
}
const selected = maplace.getSnapshot().selectedId ?? "";
for (const radio of choices.querySelectorAll("input")) radio.checked = radio.value === selected;
};
render();
maplace.subscribe(render);
return fieldset;
}
const maplace = createMaplace({ locations });
const adapter = createMapLibreAdapter();
maplace.attach(adapter, "#custom-list-maplibre-vanilla-map");
adapter.mountControl(checksMenu(maplace, "Choose a location:"), "top-right");<!doctype html>
<!-- The page vanilla.ts runs on: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->Google's bootstrap loader, with your API key: from Google's docs
<script>
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "YOUR_KEY",
v: "weekly",
});
</script><style>
#custom-list-vanilla-map { height: 24rem; }
#custom-list-vanilla-map fieldset { border: 0; padding: 0; }
#custom-list-vanilla-map fieldset label { display: inline-block; margin-right: 0.75rem; }
</style>
<div id="custom-list-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>import { createMaplace, type Location, listedLocations, type Maplace } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
const locations: Location[] = [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "york", lat: 53.9591, lng: -1.0815, title: "York" },
{ id: "bath", lat: 51.3811, lng: -2.359, title: "Bath" },
{ id: "oxford", lat: 51.752, lng: -1.2577, title: "Oxford" },
{ id: "cambridge", lat: 52.2053, lng: 0.1218, title: "Cambridge", listed: false },
];
/** A radio choosing `id`, or fitting every location when `id` is empty. */
function choice(maplace: Maplace, id: string, title: string): HTMLLabelElement {
const radio = document.createElement("input");
radio.type = "radio";
radio.name = "custom-list-vanilla-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;
}
/**
* A list of your own: a radio per location Maplace.js's lists would list, and one to fit them all.
* The choices are made again when those change; the selection is checked.
*/
function checksMenu(maplace: Maplace, heading: string): HTMLFieldSetElement {
const fieldset = document.createElement("fieldset");
const legend = document.createElement("legend");
legend.textContent = heading;
const choices = document.createElement("div");
fieldset.append(legend, choices);
let listedIds: string | undefined;
const render = () => {
const listed = listedLocations(maplace.getSnapshot());
const ids = listed.map(({ id }) => id).join();
if (ids !== listedIds) {
listedIds = ids;
const entries = listed.map(({ id, title }) => choice(maplace, id, title ?? id));
choices.replaceChildren(choice(maplace, "", "Fit all"), ...entries);
}
const selected = maplace.getSnapshot().selectedId ?? "";
for (const radio of choices.querySelectorAll("input")) radio.checked = radio.value === selected;
};
render();
maplace.subscribe(render);
return fieldset;
}
const maplace = createMaplace({ locations });
const adapter = createGoogleAdapter();
maplace.attach(adapter, "#custom-list-vanilla-map");
adapter.mountControl(checksMenu(maplace, "Choose a location:"), "top-right");<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#custom-list-maplibre-react-root .map { height: 24rem; }
#custom-list-maplibre-react-root fieldset { border: 0; padding: 0; }
#custom-list-maplibre-react-root fieldset label { display: inline-block; margin-right: 0.75rem; }
</style>
<div id="custom-list-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>import "maplibre-gl/dist/maplibre-gl.css";
import { type Location, listedLocations } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { MapControl, MaplaceMap, MaplaceProvider, useMaplace } from "@maplacejs/maplace/react";
import { StrictMode, useSyncExternalStore } from "react";
import { createRoot } from "react-dom/client";
const cities: Location[] = [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "york", lat: 53.9591, lng: -1.0815, title: "York" },
{ id: "bath", lat: 51.3811, lng: -2.359, title: "Bath" },
{ id: "oxford", lat: 51.752, lng: -1.2577, title: "Oxford" },
{ id: "cambridge", lat: 52.2053, lng: 0.1218, title: "Cambridge", listed: false },
];
/**
* A list of your own: a radio per location Maplace.js's lists would list, and one to fit them all;
* the selection is checked.
*/
function ChecksMenu({ heading }: { readonly heading: string }) {
const maplace = useMaplace();
const snapshot = useSyncExternalStore(maplace.subscribe, maplace.getSnapshot);
const choose = (id: string) => {
maplace.select(id || undefined);
if (!id) maplace.fitAll();
};
const choices = [{ id: "", title: "Fit all" }, ...listedLocations(snapshot)];
return (
<fieldset>
<legend>{heading}</legend>
<div>
{choices.map(({ id, title }) => (
<label key={id}>
<input
type="radio"
name="custom-list-maplibre-react-choice"
value={id}
checked={(snapshot.selectedId ?? "") === id}
onChange={() => choose(id)}
/>{" "}
{title ?? id}
</label>
))}
</div>
</fieldset>
);
}
function CustomList() {
return (
<MaplaceProvider locations={cities}>
<MaplaceMap createAdapter={createMapLibreAdapter} className="map">
<MapControl position="top-right">
<ChecksMenu heading="Choose a location:" />
</MapControl>
</MaplaceMap>
</MaplaceProvider>
);
}
createRoot(document.getElementById("custom-list-maplibre-react-root") as HTMLElement).render(
<StrictMode>
<CustomList />
</StrictMode>,
);<!doctype html>
<!-- The page react.tsx renders into: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->Google's bootstrap loader, with your API key: from Google's docs
<script>
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "YOUR_KEY",
v: "weekly",
});
</script><style>
#custom-list-react-root .map { height: 24rem; }
#custom-list-react-root fieldset { border: 0; padding: 0; }
#custom-list-react-root fieldset label { display: inline-block; margin-right: 0.75rem; }
</style>
<div id="custom-list-react-root"></div>
<script type="module" src="./react.tsx"></script>import { type Location, listedLocations } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { MapControl, MaplaceMap, MaplaceProvider, useMaplace } from "@maplacejs/maplace/react";
import { StrictMode, useSyncExternalStore } from "react";
import { createRoot } from "react-dom/client";
const cities: Location[] = [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "york", lat: 53.9591, lng: -1.0815, title: "York" },
{ id: "bath", lat: 51.3811, lng: -2.359, title: "Bath" },
{ id: "oxford", lat: 51.752, lng: -1.2577, title: "Oxford" },
{ id: "cambridge", lat: 52.2053, lng: 0.1218, title: "Cambridge", listed: false },
];
/**
* A list of your own: a radio per location Maplace.js's lists would list, and one to fit them all;
* the selection is checked.
*/
function ChecksMenu({ heading }: { readonly heading: string }) {
const maplace = useMaplace();
const snapshot = useSyncExternalStore(maplace.subscribe, maplace.getSnapshot);
const choose = (id: string) => {
maplace.select(id || undefined);
if (!id) maplace.fitAll();
};
const choices = [{ id: "", title: "Fit all" }, ...listedLocations(snapshot)];
return (
<fieldset>
<legend>{heading}</legend>
<div>
{choices.map(({ id, title }) => (
<label key={id}>
<input
type="radio"
name="custom-list-react-choice"
value={id}
checked={(snapshot.selectedId ?? "") === id}
onChange={() => choose(id)}
/>{" "}
{title ?? id}
</label>
))}
</div>
</fieldset>
);
}
function CustomList() {
return (
<MaplaceProvider locations={cities}>
<MaplaceMap createAdapter={createGoogleAdapter} className="map">
<MapControl position="top-right">
<ChecksMenu heading="Choose a location:" />
</MapControl>
</MaplaceMap>
</MaplaceProvider>
);
}
createRoot(document.getElementById("custom-list-react-root") as HTMLElement).render(
<StrictMode>
<CustomList />
</StrictMode>,
);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.defineListViewon the CDN script, and name it inlist:checkshere,maplibre-checkson the MapLibre page, since one document defines a name once. The factory gets the store andlisted(), the locations Maplace.js’s own lists would show, and returns{ element, destroy };list-positionplaces 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.listedLocationsand the adapter’smountControl, as below. - Bundler: redraw the element from
listedLocations(snapshot)whenever the store’ssubscribecalls back, and put it on the map with the adapter’smountControl(node, position). - React: a component reading the store with
useMaplace()anduseSyncExternalStore, inside a<MapControl>.