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.
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/maplaceMapLibre
npm install @maplacejs/maplace maplibre-glCDN 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.
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.
06 Filter by tags
Places with several tags: tick some to see those with all of them.
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-countson<maplace-map>for the numbers (Map attributes), and a script calling the element’smaplace.setFilter. - Script and Bundler:
tags: ["parking", "open-late"]on a location (Every location),groupCountson the list (List view options), andsetFilter({ tags })with what is ticked: no tags ask for nothing, so no branch is needed for none. - React:
<LocationList groupCounts />and thefilterprop 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
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>
<style>
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#offices-maplibre-map { height: 36rem; }
#offices-maplibre-map > .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<!-- A company's offices around the world, each in a region and a country (a group path, as JSON in the group attribute): the list nests the countries under their regions, with how many each holds, and a checkbox for each region and country above it filters the map and the list. -->
<maplace-map id="offices-maplibre-map" list list-heading="Offices" group-counts group-filter search
search-label="Find an office">
<maplace-location location-id="milan" lat="45.46" lng="9.19" title="Milan" group='["Europe","Italy"]'></maplace-location>
<maplace-location location-id="rome" lat="41.9" lng="12.5" title="Rome" group='["Europe","Italy"]'></maplace-location>
<maplace-location location-id="turin" lat="45.07" lng="7.69" title="Turin" group='["Europe","Italy"]'></maplace-location>
<maplace-location location-id="paris" lat="48.86" lng="2.35" title="Paris" group='["Europe","France"]'></maplace-location>
<maplace-location location-id="lyon" lat="45.76" lng="4.84" title="Lyon" group='["Europe","France"]'></maplace-location>
<maplace-location location-id="london" lat="51.51" lng="-0.13" title="London" group='["Europe","United Kingdom"]'></maplace-location>
<maplace-location location-id="manchester" lat="53.48" lng="-2.24" title="Manchester" group='["Europe","United Kingdom"]'></maplace-location>
<maplace-location location-id="tokyo" lat="35.68" lng="139.69" title="Tokyo" group='["Asia","Japan"]'></maplace-location>
<maplace-location location-id="osaka" lat="34.69" lng="135.5" title="Osaka" group='["Asia","Japan"]'></maplace-location>
<maplace-location location-id="singapore" lat="1.29" lng="103.85" title="Singapore" group='["Asia","Singapore"]'></maplace-location>
<maplace-location location-id="new-york" lat="40.71" lng="-74.01" title="New York" group='["Americas","United States"]'></maplace-location>
<maplace-location location-id="chicago" lat="41.88" lng="-87.63" title="Chicago" group='["Americas","United States"]'></maplace-location>
<maplace-location location-id="san-francisco" lat="37.77" lng="-122.42" title="San Francisco" group='["Americas","United States"]'></maplace-location>
<maplace-location location-id="sao-paulo" lat="-23.55" lng="-46.63" title="São Paulo" group='["Americas","Brazil"]'></maplace-location>
<maplace-location location-id="rio" lat="-22.91" lng="-43.17" title="Rio de Janeiro" group='["Americas","Brazil"]'></maplace-location>
<maplace-location location-id="recife" lat="-8.05" lng="-34.88" title="Recife" group='["Americas","Brazil"]'></maplace-location>
</maplace-map><!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#offices-map { height: 36rem; }
#offices-map > .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<!-- A company's offices around the world, each in a region and a country (a group path, as JSON in the group attribute): the list nests the countries under their regions, with how many each holds, and a checkbox for each region and country above it filters the map and the list. -->
<maplace-map id="offices-map" api-key="YOUR_KEY" list list-heading="Offices" group-counts group-filter search
search-label="Find an office">
<maplace-location location-id="milan" lat="45.46" lng="9.19" title="Milan" group='["Europe","Italy"]'></maplace-location>
<maplace-location location-id="rome" lat="41.9" lng="12.5" title="Rome" group='["Europe","Italy"]'></maplace-location>
<maplace-location location-id="turin" lat="45.07" lng="7.69" title="Turin" group='["Europe","Italy"]'></maplace-location>
<maplace-location location-id="paris" lat="48.86" lng="2.35" title="Paris" group='["Europe","France"]'></maplace-location>
<maplace-location location-id="lyon" lat="45.76" lng="4.84" title="Lyon" group='["Europe","France"]'></maplace-location>
<maplace-location location-id="london" lat="51.51" lng="-0.13" title="London" group='["Europe","United Kingdom"]'></maplace-location>
<maplace-location location-id="manchester" lat="53.48" lng="-2.24" title="Manchester" group='["Europe","United Kingdom"]'></maplace-location>
<maplace-location location-id="tokyo" lat="35.68" lng="139.69" title="Tokyo" group='["Asia","Japan"]'></maplace-location>
<maplace-location location-id="osaka" lat="34.69" lng="135.5" title="Osaka" group='["Asia","Japan"]'></maplace-location>
<maplace-location location-id="singapore" lat="1.29" lng="103.85" title="Singapore" group='["Asia","Singapore"]'></maplace-location>
<maplace-location location-id="new-york" lat="40.71" lng="-74.01" title="New York" group='["Americas","United States"]'></maplace-location>
<maplace-location location-id="chicago" lat="41.88" lng="-87.63" title="Chicago" group='["Americas","United States"]'></maplace-location>
<maplace-location location-id="san-francisco" lat="37.77" lng="-122.42" title="San Francisco" group='["Americas","United States"]'></maplace-location>
<maplace-location location-id="sao-paulo" lat="-23.55" lng="-46.63" title="São Paulo" group='["Americas","Brazil"]'></maplace-location>
<maplace-location location-id="rio" lat="-22.91" lng="-43.17" title="Rio de Janeiro" group='["Americas","Brazil"]'></maplace-location>
<maplace-location location-id="recife" lat="-8.05" lng="-34.88" title="Recife" group='["Americas","Brazil"]'></maplace-location>
</maplace-map><!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet and what the map and its list mount on, and the map made with Maplace; the offices by region and country, a checkbox for each above the list. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
#offices-maplibre-script-map-and-list { display: flex; height: 24rem; }
#offices-maplibre-script-map { flex: 1 1 auto; min-width: 0; }
#offices-maplibre-script-list { display: contents; }
#offices-maplibre-script-list .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#offices-maplibre-script-map-and-list { flex-direction: column; height: 36rem; }
#offices-maplibre-script-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="offices-maplibre-script-map-and-list">
<div id="offices-maplibre-script-map"></div>
<div id="offices-maplibre-script-list"></div>
</div>
<script type="module">
// Each office in a region and a country: a group path, outermost first.
const offices = await fetch("/examples/data/offices.json").then((response) => response.json());
Maplace.createMapLibreMap("#offices-maplibre-script-map", {
locations: offices,
list: {
container: "#offices-maplibre-script-list",
heading: "Offices",
groupCounts: true,
groupFilter: true,
search: true,
searchLabel: "Find an office",
},
});
</script><!doctype html>
<!-- A page of your own script: the CDN script and what the map and its list mount on, and the map made with Maplace, which adds Maps JS with your key; the offices by region and country, a checkbox for each above the list. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
#offices-script-map-and-list { display: flex; height: 24rem; }
#offices-script-map { flex: 1 1 auto; min-width: 0; }
#offices-script-list { display: contents; }
#offices-script-list .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#offices-script-map-and-list { flex-direction: column; height: 36rem; }
#offices-script-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="offices-script-map-and-list">
<div id="offices-script-map"></div>
<div id="offices-script-list"></div>
</div>
<script type="module">
// Each office in a region and a country: a group path, outermost first.
const offices = await fetch("/examples/data/offices.json").then((response) => response.json());
Maplace.createGoogleMap("#offices-script-map", {
apiKey: "YOUR_KEY",
locations: offices,
list: {
container: "#offices-script-list",
heading: "Offices",
groupCounts: true,
groupFilter: true,
search: true,
searchLabel: "Find an office",
},
});
</script><!doctype html>
<!-- The page vanilla.ts runs on: what the map and its list mount on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#offices-maplibre-vanilla-map-and-list { display: flex; height: 24rem; }
#offices-maplibre-vanilla-map { flex: 1 1 auto; min-width: 0; }
#offices-maplibre-vanilla-list { display: contents; }
#offices-maplibre-vanilla-list .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#offices-maplibre-vanilla-map-and-list { flex-direction: column; height: 36rem; }
#offices-maplibre-vanilla-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="offices-maplibre-vanilla-map-and-list">
<div id="offices-maplibre-vanilla-map"></div>
<div id="offices-maplibre-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";
// Each office in a region and a country: a group path, outermost first.
const offices: Location[] = await (await fetch("/examples/data/offices.json")).json();
createMapLibreMap("#offices-maplibre-vanilla-map", {
locations: offices,
list: {
container: "#offices-maplibre-vanilla-list",
heading: "Offices",
groupCounts: true,
groupFilter: true,
search: true,
searchLabel: "Find an office",
},
});<!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>
#offices-vanilla-map-and-list { display: flex; height: 24rem; }
#offices-vanilla-map { flex: 1 1 auto; min-width: 0; }
#offices-vanilla-list { display: contents; }
#offices-vanilla-list .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#offices-vanilla-map-and-list { flex-direction: column; height: 36rem; }
#offices-vanilla-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="offices-vanilla-map-and-list">
<div id="offices-vanilla-map"></div>
<div id="offices-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import type { Location } from "@maplacejs/maplace";
import { createGoogleMap } from "@maplacejs/maplace/google";
// Each office in a region and a country: a group path, outermost first.
const offices: Location[] = await (await fetch("/examples/data/offices.json")).json();
createGoogleMap("#offices-vanilla-map", {
locations: offices,
list: {
container: "#offices-vanilla-list",
heading: "Offices",
groupCounts: true,
groupFilter: true,
search: true,
searchLabel: "Find an office",
},
});<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#offices-maplibre-react-root .map-and-list { display: flex; height: 24rem; }
#offices-maplibre-react-root .map { flex: 1 1 auto; min-width: 0; }
#offices-maplibre-react-root .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#offices-maplibre-react-root .map-and-list { flex-direction: column; height: 36rem; }
#offices-maplibre-react-root .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="offices-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
function Offices() {
// Each office in a region and a country: a group path, outermost first.
const [offices, setOffices] = useState<readonly Location[]>([]);
useEffect(() => {
fetch("/examples/data/offices.json")
.then((response) => response.json())
.then(setOffices);
}, []);
return (
<MaplaceProvider locations={offices}>
<div className="map-and-list">
<MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
<LocationList
heading="Offices"
groupCounts
groupFilter
search
searchLabel="Find an office"
/>
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("offices-maplibre-react-root") as HTMLElement).render(
<StrictMode>
<Offices />
</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>
#offices-react-root .map-and-list { display: flex; height: 24rem; }
#offices-react-root .map { flex: 1 1 auto; min-width: 0; }
#offices-react-root .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size. */
@media (width < 40rem) {
#offices-react-root .map-and-list { flex-direction: column; height: 36rem; }
#offices-react-root .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="offices-react-root"></div>
<script type="module" src="./react.tsx"></script>import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
function Offices() {
// Each office in a region and a country: a group path, outermost first.
const [offices, setOffices] = useState<readonly Location[]>([]);
useEffect(() => {
fetch("/examples/data/offices.json")
.then((response) => response.json())
.then(setOffices);
}, []);
return (
<MaplaceProvider locations={offices}>
<div className="map-and-list">
<MaplaceMap createAdapter={createGoogleAdapter} className="map" />
<LocationList
heading="Offices"
groupCounts
groupFilter
search
searchLabel="Find an office"
/>
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("offices-react-root") as HTMLElement).render(
<StrictMode>
<Offices />
</StrictMode>,
);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: agroupstarting with[is read as a path (Location attributes);group-filter,group-countsandsearchon<maplace-map>(Map attributes). - Script and Bundler:
group: ["Europe", "Italy"]on each office, up to 3 names (Every location), andgroupFilter,groupCountsandsearchon the list (List view options). The boxes callsetFilterfor you:setFilter([["Europe", "Italy"]])asks for one path, Italy’s offices, whilesetFilter(["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>).
08 Search box
A box above the list that narrows the list and the map as you type.
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>
<style>
/* On a phone the list goes under the map, its half staying that size as the places narrow. */
@media (width < 40rem) {
#search-maplibre-map { height: 36rem; }
#search-maplibre-map > .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<!-- Places in a list beside the map, under a search box: typing narrows the list and the map by name, group or search-text. -->
<maplace-map id="search-maplibre-map" list search search-label="Find a place">
<maplace-location location-id="rome" lat="41.9028" lng="12.4964" title="Rome" group="Centre"></maplace-location>
<maplace-location location-id="bologna" lat="44.4949" lng="11.3426" title="Bologna" group="Centre" search-text="pasta"></maplace-location>
<maplace-location location-id="forli" lat="44.2227" lng="12.0407" title="Forlì" group="Centre"></maplace-location>
<maplace-location location-id="milan" lat="45.4642" lng="9.19" title="Milan" group="North"></maplace-location>
<maplace-location location-id="turin" lat="45.0703" lng="7.6869" title="Turin" group="North"></maplace-location>
<maplace-location location-id="genoa" lat="44.4056" lng="8.9463" title="Genoa" group="North" search-text="pasta, harbour"></maplace-location>
<maplace-location location-id="naples" lat="40.8518" lng="14.2681" title="Naples" group="South" search-text="pizza, harbour"></maplace-location>
<maplace-location location-id="bari" lat="41.1171" lng="16.8719" title="Bari" group="South" search-text="harbour"></maplace-location>
</maplace-map><!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
/* On a phone the list goes under the map, its half staying that size as the places narrow. */
@media (width < 40rem) {
#search-map { height: 36rem; }
#search-map > .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<!-- Places in a list beside the map, under a search box: typing narrows the list and the map by name, group or search-text. -->
<maplace-map id="search-map" api-key="YOUR_KEY" list search search-label="Find a place">
<maplace-location location-id="rome" lat="41.9028" lng="12.4964" title="Rome" group="Centre"></maplace-location>
<maplace-location location-id="bologna" lat="44.4949" lng="11.3426" title="Bologna" group="Centre" search-text="pasta"></maplace-location>
<maplace-location location-id="forli" lat="44.2227" lng="12.0407" title="Forlì" group="Centre"></maplace-location>
<maplace-location location-id="milan" lat="45.4642" lng="9.19" title="Milan" group="North"></maplace-location>
<maplace-location location-id="turin" lat="45.0703" lng="7.6869" title="Turin" group="North"></maplace-location>
<maplace-location location-id="genoa" lat="44.4056" lng="8.9463" title="Genoa" group="North" search-text="pasta, harbour"></maplace-location>
<maplace-location location-id="naples" lat="40.8518" lng="14.2681" title="Naples" group="South" search-text="pizza, harbour"></maplace-location>
<maplace-location location-id="bari" lat="41.1171" lng="16.8719" title="Bari" group="South" search-text="harbour"></maplace-location>
</maplace-map><!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet and what the map and its list mount on, and the map made with Maplace. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
#search-maplibre-script-map-and-list { display: flex; height: 24rem; }
#search-maplibre-script-map { flex: 1 1 auto; min-width: 0; }
#search-maplibre-script-list { display: contents; }
#search-maplibre-script-list .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size as the places narrow. */
@media (width < 40rem) {
#search-maplibre-script-map-and-list { flex-direction: column; height: 36rem; }
#search-maplibre-script-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="search-maplibre-script-map-and-list">
<div id="search-maplibre-script-map"></div>
<div id="search-maplibre-script-list"></div>
</div>
<script type="module">
Maplace.createMapLibreMap("#search-maplibre-script-map", {
locations: [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome", group: "Centre" },
{ id: "bologna", lat: 44.4949, lng: 11.3426, title: "Bologna", group: "Centre", searchText: "pasta" },
{ id: "forli", lat: 44.2227, lng: 12.0407, title: "Forlì", group: "Centre" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan", group: "North" },
{ id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin", group: "North" },
{
id: "genoa",
lat: 44.4056,
lng: 8.9463,
title: "Genoa",
group: "North",
searchText: "pasta, harbour",
},
{
id: "naples",
lat: 40.8518,
lng: 14.2681,
title: "Naples",
group: "South",
searchText: "pizza, harbour",
},
{ id: "bari", lat: 41.1171, lng: 16.8719, title: "Bari", group: "South", searchText: "harbour" },
],
list: { container: "#search-maplibre-script-list", search: true, searchLabel: "Find a place" },
});
</script><!doctype html>
<!-- A page of your own script: the CDN script and what the map and its list mount on, and the map made with Maplace, which adds Maps JS with your key. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
#search-script-map-and-list { display: flex; height: 24rem; }
#search-script-map { flex: 1 1 auto; min-width: 0; }
#search-script-list { display: contents; }
#search-script-list .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size as the places narrow. */
@media (width < 40rem) {
#search-script-map-and-list { flex-direction: column; height: 36rem; }
#search-script-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="search-script-map-and-list">
<div id="search-script-map"></div>
<div id="search-script-list"></div>
</div>
<script type="module">
Maplace.createGoogleMap("#search-script-map", {
apiKey: "YOUR_KEY",
locations: [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome", group: "Centre" },
{ id: "bologna", lat: 44.4949, lng: 11.3426, title: "Bologna", group: "Centre", searchText: "pasta" },
{ id: "forli", lat: 44.2227, lng: 12.0407, title: "Forlì", group: "Centre" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan", group: "North" },
{ id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin", group: "North" },
{
id: "genoa",
lat: 44.4056,
lng: 8.9463,
title: "Genoa",
group: "North",
searchText: "pasta, harbour",
},
{
id: "naples",
lat: 40.8518,
lng: 14.2681,
title: "Naples",
group: "South",
searchText: "pizza, harbour",
},
{ id: "bari", lat: 41.1171, lng: 16.8719, title: "Bari", group: "South", searchText: "harbour" },
],
list: { container: "#search-script-list", search: true, searchLabel: "Find a place" },
});
</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>
#search-maplibre-vanilla-map-and-list { display: flex; height: 24rem; }
#search-maplibre-vanilla-map { flex: 1 1 auto; min-width: 0; }
#search-maplibre-vanilla-list { display: contents; }
#search-maplibre-vanilla-list .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size as the places narrow. */
@media (width < 40rem) {
#search-maplibre-vanilla-map-and-list { flex-direction: column; height: 36rem; }
#search-maplibre-vanilla-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="search-maplibre-vanilla-map-and-list">
<div id="search-maplibre-vanilla-map"></div>
<div id="search-maplibre-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import "maplibre-gl/dist/maplibre-gl.css";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";
createMapLibreMap("#search-maplibre-vanilla-map", {
locations: [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome", group: "Centre" },
{
id: "bologna",
lat: 44.4949,
lng: 11.3426,
title: "Bologna",
group: "Centre",
searchText: "pasta",
},
{ id: "forli", lat: 44.2227, lng: 12.0407, title: "Forlì", group: "Centre" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan", group: "North" },
{ id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin", group: "North" },
{
id: "genoa",
lat: 44.4056,
lng: 8.9463,
title: "Genoa",
group: "North",
searchText: "pasta, harbour",
},
{
id: "naples",
lat: 40.8518,
lng: 14.2681,
title: "Naples",
group: "South",
searchText: "pizza, harbour",
},
{
id: "bari",
lat: 41.1171,
lng: 16.8719,
title: "Bari",
group: "South",
searchText: "harbour",
},
],
list: {
container: "#search-maplibre-vanilla-list",
search: true,
searchLabel: "Find a place",
},
});<!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>
#search-vanilla-map-and-list { display: flex; height: 24rem; }
#search-vanilla-map { flex: 1 1 auto; min-width: 0; }
#search-vanilla-list { display: contents; }
#search-vanilla-list .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size as the places narrow. */
@media (width < 40rem) {
#search-vanilla-map-and-list { flex-direction: column; height: 36rem; }
#search-vanilla-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="search-vanilla-map-and-list">
<div id="search-vanilla-map"></div>
<div id="search-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import { createGoogleMap } from "@maplacejs/maplace/google";
createGoogleMap("#search-vanilla-map", {
locations: [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome", group: "Centre" },
{
id: "bologna",
lat: 44.4949,
lng: 11.3426,
title: "Bologna",
group: "Centre",
searchText: "pasta",
},
{ id: "forli", lat: 44.2227, lng: 12.0407, title: "Forlì", group: "Centre" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan", group: "North" },
{ id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin", group: "North" },
{
id: "genoa",
lat: 44.4056,
lng: 8.9463,
title: "Genoa",
group: "North",
searchText: "pasta, harbour",
},
{
id: "naples",
lat: 40.8518,
lng: 14.2681,
title: "Naples",
group: "South",
searchText: "pizza, harbour",
},
{
id: "bari",
lat: 41.1171,
lng: 16.8719,
title: "Bari",
group: "South",
searchText: "harbour",
},
],
list: {
container: "#search-vanilla-list",
search: true,
searchLabel: "Find a place",
},
});<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#search-maplibre-react-root .map-and-list { display: flex; height: 24rem; }
#search-maplibre-react-root .map { flex: 1 1 auto; min-width: 0; }
#search-maplibre-react-root .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size as the places narrow. */
@media (width < 40rem) {
#search-maplibre-react-root .map-and-list { flex-direction: column; height: 36rem; }
#search-maplibre-react-root .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="search-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
const cities: Location[] = [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome", group: "Centre" },
{
id: "bologna",
lat: 44.4949,
lng: 11.3426,
title: "Bologna",
group: "Centre",
searchText: "pasta",
},
{ id: "forli", lat: 44.2227, lng: 12.0407, title: "Forlì", group: "Centre" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan", group: "North" },
{ id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin", group: "North" },
{
id: "genoa",
lat: 44.4056,
lng: 8.9463,
title: "Genoa",
group: "North",
searchText: "pasta, harbour",
},
{
id: "naples",
lat: 40.8518,
lng: 14.2681,
title: "Naples",
group: "South",
searchText: "pizza, harbour",
},
{ id: "bari", lat: 41.1171, lng: 16.8719, title: "Bari", group: "South", searchText: "harbour" },
];
function Search() {
return (
<MaplaceProvider locations={cities}>
<div className="map-and-list">
<MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
<LocationList search searchLabel="Find a place" />
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("search-maplibre-react-root") as HTMLElement).render(
<StrictMode>
<Search />
</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>
#search-react-root .map-and-list { display: flex; height: 24rem; }
#search-react-root .map { flex: 1 1 auto; min-width: 0; }
#search-react-root .maplace-list { flex: 0 1 16rem; }
/* On a phone the list goes under the map, its half staying that size as the places narrow. */
@media (width < 40rem) {
#search-react-root .map-and-list { flex-direction: column; height: 36rem; }
#search-react-root .maplace-list { flex: 0 0 50%; max-height: 50%; }
}
</style>
<div id="search-react-root"></div>
<script type="module" src="./react.tsx"></script>import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
const cities: Location[] = [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome", group: "Centre" },
{
id: "bologna",
lat: 44.4949,
lng: 11.3426,
title: "Bologna",
group: "Centre",
searchText: "pasta",
},
{ id: "forli", lat: 44.2227, lng: 12.0407, title: "Forlì", group: "Centre" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan", group: "North" },
{ id: "turin", lat: 45.0703, lng: 7.6869, title: "Turin", group: "North" },
{
id: "genoa",
lat: 44.4056,
lng: 8.9463,
title: "Genoa",
group: "North",
searchText: "pasta, harbour",
},
{
id: "naples",
lat: 40.8518,
lng: 14.2681,
title: "Naples",
group: "South",
searchText: "pizza, harbour",
},
{ id: "bari", lat: 41.1171, lng: 16.8719, title: "Bari", group: "South", searchText: "harbour" },
];
function Search() {
return (
<MaplaceProvider locations={cities}>
<div className="map-and-list">
<MaplaceMap createAdapter={createGoogleAdapter} className="map" />
<LocationList search searchLabel="Find a place" />
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("search-react-root") as HTMLElement).render(
<StrictMode>
<Search />
</StrictMode>,
);A box above the list narrows the list and the map when your typing pauses: by a place’s name, its group or its searchText, ignoring case and accents. A line under the box says how many are found, or that none is. The map doesn’t move: choose an entry and it pans there.
- Try it: type “forli” to find Forlì, or “harbour” to find the places with that word in their
searchText. - HTML:
searchon<maplace-map>turns the box on andsearch-labelsets its placeholder, whileno-matches-labelandmatches-labelset what the line under it says when nothing matches and before the count (Map attributes), andsearch-texton a<maplace-location>is what it also looks in (Location attributes). - Script and Bundler:
list: { container, search: true, searchLabel: "Find a place" }in the map’s options (List view options), andsearchTexton a location (Every location). - React:
<LocationList search searchLabel="Find a place" />beside the<MaplaceMap>(<LocationList>). - Your own box: the built-in box calls
setFilteronce typing pauses, so the map doesn’t redraw under each key, and keeps the rest of the filter.setFilteritself replaces the whole filter: a box or checkboxes of your own spreadgetSnapshot().filterto keep the query.
09 Nearest first
A button on the map that sorts the list by distance from the visitor.
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>
<!-- Places in a list beside the map; the "My location" button on the map sorts it nearest first, each entry saying how far. -->
<maplace-map id="nearest-maplibre-map" list list-sort="distance" my-location>
<maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London"></maplace-location>
<maplace-location location-id="bristol" lat="51.4545" lng="-2.5879" title="Bristol"></maplace-location>
<maplace-location location-id="birmingham" lat="52.4862" lng="-1.8904" title="Birmingham"></maplace-location>
<maplace-location location-id="manchester" lat="53.4808" lng="-2.2426" title="Manchester"></maplace-location>
<maplace-location location-id="leeds" lat="53.8008" lng="-1.5491" title="Leeds"></maplace-location>
</maplace-map><!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<!-- Places in a list beside the map; the "My location" button on the map sorts it nearest first, each entry saying how far. -->
<maplace-map id="nearest-map" api-key="YOUR_KEY" list list-sort="distance" my-location>
<maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London"></maplace-location>
<maplace-location location-id="bristol" lat="51.4545" lng="-2.5879" title="Bristol"></maplace-location>
<maplace-location location-id="birmingham" lat="52.4862" lng="-1.8904" title="Birmingham"></maplace-location>
<maplace-location location-id="manchester" lat="53.4808" lng="-2.2426" title="Manchester"></maplace-location>
<maplace-location location-id="leeds" lat="53.8008" lng="-1.5491" title="Leeds"></maplace-location>
</maplace-map><!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet, what the map and its list mount on, and the map made with Maplace. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
#nearest-maplibre-script-map-and-list { display: flex; height: 24rem; }
#nearest-maplibre-script-map { flex: 1 1 auto; min-width: 0; }
#nearest-maplibre-script-list { display: contents; }
#nearest-maplibre-script-list [role="listbox"] { flex: 0 1 16rem; }
/* On a phone the list goes under the map, as the element's does. */
@media (width < 40rem) {
#nearest-maplibre-script-map-and-list { flex-direction: column; }
#nearest-maplibre-script-list [role="listbox"] { flex: none; max-height: 40%; }
}
</style>
<div id="nearest-maplibre-script-map-and-list">
<div id="nearest-maplibre-script-map"></div>
<div id="nearest-maplibre-script-list"></div>
</div>
<script type="module">
Maplace.createMapLibreMap("#nearest-maplibre-script-map", {
locations: [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "bristol", lat: 51.4545, lng: -2.5879, title: "Bristol" },
{ id: "birmingham", lat: 52.4862, lng: -1.8904, title: "Birmingham" },
{ id: "manchester", lat: 53.4808, lng: -2.2426, title: "Manchester" },
{ id: "leeds", lat: 53.8008, lng: -1.5491, title: "Leeds" },
],
list: { container: "#nearest-maplibre-script-list", sort: "distance" },
myLocation: true,
});
</script><!doctype html>
<!-- A page of your own script: the CDN script, what the map and its list mount on, and the map made with Maplace, which adds Maps JS with your key. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
#nearest-script-map-and-list { display: flex; height: 24rem; }
#nearest-script-map { flex: 1 1 auto; min-width: 0; }
#nearest-script-list { display: contents; }
#nearest-script-list [role="listbox"] { flex: 0 1 16rem; }
/* On a phone the list goes under the map, as the element's does. */
@media (width < 40rem) {
#nearest-script-map-and-list { flex-direction: column; }
#nearest-script-list [role="listbox"] { flex: none; max-height: 40%; }
}
</style>
<div id="nearest-script-map-and-list">
<div id="nearest-script-map"></div>
<div id="nearest-script-list"></div>
</div>
<script type="module">
Maplace.createGoogleMap("#nearest-script-map", {
apiKey: "YOUR_KEY",
locations: [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "bristol", lat: 51.4545, lng: -2.5879, title: "Bristol" },
{ id: "birmingham", lat: 52.4862, lng: -1.8904, title: "Birmingham" },
{ id: "manchester", lat: 53.4808, lng: -2.2426, title: "Manchester" },
{ id: "leeds", lat: 53.8008, lng: -1.5491, title: "Leeds" },
],
list: { container: "#nearest-script-list", sort: "distance" },
myLocation: true,
});
</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>
#nearest-maplibre-vanilla-map-and-list { display: flex; height: 24rem; }
#nearest-maplibre-vanilla-map { flex: 1 1 auto; min-width: 0; }
#nearest-maplibre-vanilla-list { display: contents; }
#nearest-maplibre-vanilla-list [role="listbox"] { flex: 0 1 16rem; }
/* On a phone the list goes under the map, as the element's does. */
@media (width < 40rem) {
#nearest-maplibre-vanilla-map-and-list { flex-direction: column; }
#nearest-maplibre-vanilla-list [role="listbox"] { flex: none; max-height: 40%; }
}
</style>
<div id="nearest-maplibre-vanilla-map-and-list">
<div id="nearest-maplibre-vanilla-map"></div>
<div id="nearest-maplibre-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import "maplibre-gl/dist/maplibre-gl.css";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";
createMapLibreMap("#nearest-maplibre-vanilla-map", {
locations: [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "bristol", lat: 51.4545, lng: -2.5879, title: "Bristol" },
{ id: "birmingham", lat: 52.4862, lng: -1.8904, title: "Birmingham" },
{ id: "manchester", lat: 53.4808, lng: -2.2426, title: "Manchester" },
{ id: "leeds", lat: 53.8008, lng: -1.5491, title: "Leeds" },
],
list: { container: "#nearest-maplibre-vanilla-list", sort: "distance" },
myLocation: true,
});<!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>
#nearest-vanilla-map-and-list { display: flex; height: 24rem; }
#nearest-vanilla-map { flex: 1 1 auto; min-width: 0; }
#nearest-vanilla-list { display: contents; }
#nearest-vanilla-list [role="listbox"] { flex: 0 1 16rem; }
/* On a phone the list goes under the map, as the element's does. */
@media (width < 40rem) {
#nearest-vanilla-map-and-list { flex-direction: column; }
#nearest-vanilla-list [role="listbox"] { flex: none; max-height: 40%; }
}
</style>
<div id="nearest-vanilla-map-and-list">
<div id="nearest-vanilla-map"></div>
<div id="nearest-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import { createGoogleMap } from "@maplacejs/maplace/google";
createGoogleMap("#nearest-vanilla-map", {
locations: [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "bristol", lat: 51.4545, lng: -2.5879, title: "Bristol" },
{ id: "birmingham", lat: 52.4862, lng: -1.8904, title: "Birmingham" },
{ id: "manchester", lat: 53.4808, lng: -2.2426, title: "Manchester" },
{ id: "leeds", lat: 53.8008, lng: -1.5491, title: "Leeds" },
],
list: { container: "#nearest-vanilla-list", sort: "distance" },
myLocation: true,
});<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#nearest-maplibre-react-root .map-and-list { display: flex; height: 24rem; }
#nearest-maplibre-react-root .map { flex: 1 1 auto; min-width: 0; }
#nearest-maplibre-react-root [role="listbox"] { flex: 0 1 16rem; }
/* On a phone the list goes under the map, as the element's does. */
@media (width < 40rem) {
#nearest-maplibre-react-root .map-and-list { flex-direction: column; }
#nearest-maplibre-react-root [role="listbox"] { flex: none; max-height: 40%; }
}
</style>
<div id="nearest-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { LocationList, MaplaceMap, MaplaceProvider, MyLocation } from "@maplacejs/maplace/react";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
const cities: Location[] = [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "bristol", lat: 51.4545, lng: -2.5879, title: "Bristol" },
{ id: "birmingham", lat: 52.4862, lng: -1.8904, title: "Birmingham" },
{ id: "manchester", lat: 53.4808, lng: -2.2426, title: "Manchester" },
{ id: "leeds", lat: 53.8008, lng: -1.5491, title: "Leeds" },
];
function Nearest() {
return (
<MaplaceProvider locations={cities}>
<div className="map-and-list">
<MaplaceMap createAdapter={createMapLibreAdapter} className="map">
<MyLocation />
</MaplaceMap>
<LocationList sort="distance" />
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("nearest-maplibre-react-root") as HTMLElement).render(
<StrictMode>
<Nearest />
</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>
#nearest-react-root .map-and-list { display: flex; height: 24rem; }
#nearest-react-root .map { flex: 1 1 auto; min-width: 0; }
#nearest-react-root [role="listbox"] { flex: 0 1 16rem; }
/* On a phone the list goes under the map, as the element's does. */
@media (width < 40rem) {
#nearest-react-root .map-and-list { flex-direction: column; }
#nearest-react-root [role="listbox"] { flex: none; max-height: 40%; }
}
</style>
<div id="nearest-react-root"></div>
<script type="module" src="./react.tsx"></script>import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, MaplaceMap, MaplaceProvider, MyLocation } from "@maplacejs/maplace/react";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
const cities: Location[] = [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London" },
{ id: "bristol", lat: 51.4545, lng: -2.5879, title: "Bristol" },
{ id: "birmingham", lat: 52.4862, lng: -1.8904, title: "Birmingham" },
{ id: "manchester", lat: 53.4808, lng: -2.2426, title: "Manchester" },
{ id: "leeds", lat: 53.8008, lng: -1.5491, title: "Leeds" },
];
function Nearest() {
return (
<MaplaceProvider locations={cities}>
<div className="map-and-list">
<MaplaceMap createAdapter={createGoogleAdapter} className="map">
<MyLocation />
</MaplaceMap>
<LocationList sort="distance" />
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("nearest-react-root") as HTMLElement).render(
<StrictMode>
<Nearest />
</StrictMode>,
);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"andmy-locationon<maplace-map>, withdistance-unit="mile"for miles (Map attributes). - Script and Bundler:
sort: "distance"inlist, andmyLocation: truein the map’s options (Distances). - React:
<LocationList sort="distance" />, and<MyLocation />inside the<MaplaceMap>(<MyLocation>). - Another point:
setOrigintakes 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.
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 on the map that follows it: its switch lists "All", or in "This area" what is in view as the map moves. -->
<maplace-map id="this-area-maplibre-map" list list-position="top-left" list-scope="viewport">
<maplace-location location-id="lisbon" lat="38.7223" lng="-9.1393" title="Lisbon"></maplace-location>
<maplace-location location-id="madrid" lat="40.4168" lng="-3.7038" title="Madrid"></maplace-location>
<maplace-location location-id="paris" lat="48.8566" lng="2.3522" title="Paris"></maplace-location>
<maplace-location location-id="brussels" lat="50.8503" lng="4.3517" title="Brussels"></maplace-location>
<maplace-location location-id="amsterdam" lat="52.3676" lng="4.9041" title="Amsterdam"></maplace-location>
<maplace-location location-id="berlin" lat="52.52" lng="13.405" title="Berlin"></maplace-location>
<maplace-location location-id="copenhagen" lat="55.6761" lng="12.5683" title="Copenhagen"></maplace-location>
<maplace-location location-id="vienna" lat="48.2082" lng="16.3738" title="Vienna"></maplace-location>
<maplace-location location-id="prague" lat="50.0755" lng="14.4378" title="Prague"></maplace-location>
<maplace-location location-id="warsaw" lat="52.2297" lng="21.0122" title="Warsaw"></maplace-location>
<maplace-location location-id="rome" lat="41.9028" lng="12.4964" title="Rome"></maplace-location>
<maplace-location location-id="athens" lat="37.9838" lng="23.7275" title="Athens"></maplace-location>
</maplace-map><!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<!-- A list on the map that follows it: its switch lists "All", or in "This area" what is in view as the map moves. -->
<maplace-map id="this-area-map" api-key="YOUR_KEY" list list-position="top-left" list-scope="viewport">
<maplace-location location-id="lisbon" lat="38.7223" lng="-9.1393" title="Lisbon"></maplace-location>
<maplace-location location-id="madrid" lat="40.4168" lng="-3.7038" title="Madrid"></maplace-location>
<maplace-location location-id="paris" lat="48.8566" lng="2.3522" title="Paris"></maplace-location>
<maplace-location location-id="brussels" lat="50.8503" lng="4.3517" title="Brussels"></maplace-location>
<maplace-location location-id="amsterdam" lat="52.3676" lng="4.9041" title="Amsterdam"></maplace-location>
<maplace-location location-id="berlin" lat="52.52" lng="13.405" title="Berlin"></maplace-location>
<maplace-location location-id="copenhagen" lat="55.6761" lng="12.5683" title="Copenhagen"></maplace-location>
<maplace-location location-id="vienna" lat="48.2082" lng="16.3738" title="Vienna"></maplace-location>
<maplace-location location-id="prague" lat="50.0755" lng="14.4378" title="Prague"></maplace-location>
<maplace-location location-id="warsaw" lat="52.2297" lng="21.0122" title="Warsaw"></maplace-location>
<maplace-location location-id="rome" lat="41.9028" lng="12.4964" title="Rome"></maplace-location>
<maplace-location location-id="athens" lat="37.9838" lng="23.7275" title="Athens"></maplace-location>
</maplace-map><!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. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
#this-area-maplibre-script-map { height: 24rem; }
</style>
<div id="this-area-maplibre-script-map"></div>
<script type="module">
Maplace.createMapLibreMap("#this-area-maplibre-script-map", {
locations: [
{ id: "lisbon", lat: 38.7223, lng: -9.1393, title: "Lisbon" },
{ id: "madrid", lat: 40.4168, lng: -3.7038, title: "Madrid" },
{ id: "paris", lat: 48.8566, lng: 2.3522, title: "Paris" },
{ id: "brussels", lat: 50.8503, lng: 4.3517, title: "Brussels" },
{ id: "amsterdam", lat: 52.3676, lng: 4.9041, title: "Amsterdam" },
{ id: "berlin", lat: 52.52, lng: 13.405, title: "Berlin" },
{ id: "copenhagen", lat: 55.6761, lng: 12.5683, title: "Copenhagen" },
{ id: "vienna", lat: 48.2082, lng: 16.3738, title: "Vienna" },
{ id: "prague", lat: 50.0755, lng: 14.4378, title: "Prague" },
{ id: "warsaw", lat: 52.2297, lng: 21.0122, title: "Warsaw" },
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "athens", lat: 37.9838, lng: 23.7275, title: "Athens" },
],
list: { scope: "viewport", position: "top-left" },
});
</script><!doctype html>
<!-- A page of your own script: the CDN script, what the map mounts on, and the map made with Maplace, which adds Maps JS with your key. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
#this-area-script-map { height: 24rem; }
</style>
<div id="this-area-script-map"></div>
<script type="module">
Maplace.createGoogleMap("#this-area-script-map", {
apiKey: "YOUR_KEY",
locations: [
{ id: "lisbon", lat: 38.7223, lng: -9.1393, title: "Lisbon" },
{ id: "madrid", lat: 40.4168, lng: -3.7038, title: "Madrid" },
{ id: "paris", lat: 48.8566, lng: 2.3522, title: "Paris" },
{ id: "brussels", lat: 50.8503, lng: 4.3517, title: "Brussels" },
{ id: "amsterdam", lat: 52.3676, lng: 4.9041, title: "Amsterdam" },
{ id: "berlin", lat: 52.52, lng: 13.405, title: "Berlin" },
{ id: "copenhagen", lat: 55.6761, lng: 12.5683, title: "Copenhagen" },
{ id: "vienna", lat: 48.2082, lng: 16.3738, title: "Vienna" },
{ id: "prague", lat: 50.0755, lng: 14.4378, title: "Prague" },
{ id: "warsaw", lat: 52.2297, lng: 21.0122, title: "Warsaw" },
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "athens", lat: 37.9838, lng: 23.7275, title: "Athens" },
],
list: { scope: "viewport", position: "top-left" },
});
</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>
#this-area-maplibre-vanilla-map { height: 24rem; }
</style>
<div id="this-area-maplibre-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>import "maplibre-gl/dist/maplibre-gl.css";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";
createMapLibreMap("#this-area-maplibre-vanilla-map", {
locations: [
{ id: "lisbon", lat: 38.7223, lng: -9.1393, title: "Lisbon" },
{ id: "madrid", lat: 40.4168, lng: -3.7038, title: "Madrid" },
{ id: "paris", lat: 48.8566, lng: 2.3522, title: "Paris" },
{ id: "brussels", lat: 50.8503, lng: 4.3517, title: "Brussels" },
{ id: "amsterdam", lat: 52.3676, lng: 4.9041, title: "Amsterdam" },
{ id: "berlin", lat: 52.52, lng: 13.405, title: "Berlin" },
{ id: "copenhagen", lat: 55.6761, lng: 12.5683, title: "Copenhagen" },
{ id: "vienna", lat: 48.2082, lng: 16.3738, title: "Vienna" },
{ id: "prague", lat: 50.0755, lng: 14.4378, title: "Prague" },
{ id: "warsaw", lat: 52.2297, lng: 21.0122, title: "Warsaw" },
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "athens", lat: 37.9838, lng: 23.7275, title: "Athens" },
],
// On the map, top-left: a list switched between "All" and "This area", what is in view.
list: { scope: "viewport", position: "top-left" },
});<!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>
#this-area-vanilla-map { height: 24rem; }
</style>
<div id="this-area-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>import { createGoogleMap } from "@maplacejs/maplace/google";
createGoogleMap("#this-area-vanilla-map", {
locations: [
{ id: "lisbon", lat: 38.7223, lng: -9.1393, title: "Lisbon" },
{ id: "madrid", lat: 40.4168, lng: -3.7038, title: "Madrid" },
{ id: "paris", lat: 48.8566, lng: 2.3522, title: "Paris" },
{ id: "brussels", lat: 50.8503, lng: 4.3517, title: "Brussels" },
{ id: "amsterdam", lat: 52.3676, lng: 4.9041, title: "Amsterdam" },
{ id: "berlin", lat: 52.52, lng: 13.405, title: "Berlin" },
{ id: "copenhagen", lat: 55.6761, lng: 12.5683, title: "Copenhagen" },
{ id: "vienna", lat: 48.2082, lng: 16.3738, title: "Vienna" },
{ id: "prague", lat: 50.0755, lng: 14.4378, title: "Prague" },
{ id: "warsaw", lat: 52.2297, lng: 21.0122, title: "Warsaw" },
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "athens", lat: 37.9838, lng: 23.7275, title: "Athens" },
],
// On the map, top-left: a list switched between "All" and "This area", what is in view.
list: { scope: "viewport", position: "top-left" },
});<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#this-area-maplibre-react-root .map { height: 24rem; }
</style>
<div id="this-area-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
const capitals: Location[] = [
{ id: "lisbon", lat: 38.7223, lng: -9.1393, title: "Lisbon" },
{ id: "madrid", lat: 40.4168, lng: -3.7038, title: "Madrid" },
{ id: "paris", lat: 48.8566, lng: 2.3522, title: "Paris" },
{ id: "brussels", lat: 50.8503, lng: 4.3517, title: "Brussels" },
{ id: "amsterdam", lat: 52.3676, lng: 4.9041, title: "Amsterdam" },
{ id: "berlin", lat: 52.52, lng: 13.405, title: "Berlin" },
{ id: "copenhagen", lat: 55.6761, lng: 12.5683, title: "Copenhagen" },
{ id: "vienna", lat: 48.2082, lng: 16.3738, title: "Vienna" },
{ id: "prague", lat: 50.0755, lng: 14.4378, title: "Prague" },
{ id: "warsaw", lat: 52.2297, lng: 21.0122, title: "Warsaw" },
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "athens", lat: 37.9838, lng: 23.7275, title: "Athens" },
];
function ThisArea() {
return (
<MaplaceProvider locations={capitals}>
<MaplaceMap createAdapter={createMapLibreAdapter} className="map">
{/* A list switched between "All" and "This area", what is in view as the map moves. */}
<LocationList scope="viewport" position="top-left" />
</MaplaceMap>
</MaplaceProvider>
);
}
createRoot(document.getElementById("this-area-maplibre-react-root") as HTMLElement).render(
<StrictMode>
<ThisArea />
</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>
#this-area-react-root .map { height: 24rem; }
</style>
<div id="this-area-react-root"></div>
<script type="module" src="./react.tsx"></script>import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
const capitals: Location[] = [
{ id: "lisbon", lat: 38.7223, lng: -9.1393, title: "Lisbon" },
{ id: "madrid", lat: 40.4168, lng: -3.7038, title: "Madrid" },
{ id: "paris", lat: 48.8566, lng: 2.3522, title: "Paris" },
{ id: "brussels", lat: 50.8503, lng: 4.3517, title: "Brussels" },
{ id: "amsterdam", lat: 52.3676, lng: 4.9041, title: "Amsterdam" },
{ id: "berlin", lat: 52.52, lng: 13.405, title: "Berlin" },
{ id: "copenhagen", lat: 55.6761, lng: 12.5683, title: "Copenhagen" },
{ id: "vienna", lat: 48.2082, lng: 16.3738, title: "Vienna" },
{ id: "prague", lat: 50.0755, lng: 14.4378, title: "Prague" },
{ id: "warsaw", lat: 52.2297, lng: 21.0122, title: "Warsaw" },
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "athens", lat: 37.9838, lng: 23.7275, title: "Athens" },
];
function ThisArea() {
return (
<MaplaceProvider locations={capitals}>
<MaplaceMap createAdapter={createGoogleAdapter} className="map">
{/* A list switched between "All" and "This area", what is in view as the map moves. */}
<LocationList scope="viewport" position="top-left" />
</MaplaceMap>
</MaplaceProvider>
);
}
createRoot(document.getElementById("this-area-react-root") as HTMLElement).render(
<StrictMode>
<ThisArea />
</StrictMode>,
);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>, besidelistandlist-position, witharea-only-label,show-all-labelandempty-area-labelfor its wording (Map attributes). - Script and Bundler:
list: { scope: "viewport", position: "top-left" }in the map’s options, withareaOnlyLabel,showAllLabelandemptyAreaLabel(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-lefthere. 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 aposition, 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.
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>,
);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.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>.
12 Tabs
The list outside the map; a details panel follows the selection.
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>
<!-- The list sits outside the map, styled as tabs above it, starting on the first place; the chosen tab's details sit right below the tabs, above the map, and follow the selection. -->
<style>
#tabs-maplibre-map { flex-direction: column; }
#tabs-maplibre-map > [role="listbox"] { order: -2; display: flex; flex: none; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
#tabs-maplibre-map > [role="listbox"] > [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
#tabs-maplibre-map > [role="listbox"] > [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
#tabs-maplibre-map > #tabs-maplibre-details { order: -1; padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<maplace-map id="tabs-maplibre-map" list selected-id="london">
<maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London" zoom="6"></maplace-location>
<maplace-location location-id="edinburgh" lat="55.9533" lng="-3.1883" title="Edinburgh" zoom="6"></maplace-location>
<aside id="tabs-maplibre-details"></aside>
</maplace-map>
<script>
const tabsMapLibreNotes = { london: 'Capital of the United Kingdom.', edinburgh: 'Capital of Scotland, home of the Fringe arts festival.' };
const tabsMapLibreMap = document.getElementById('tabs-maplibre-map');
function showTabsMapLibreNote(id) {
document.getElementById('tabs-maplibre-details').textContent = tabsMapLibreNotes[id] ?? '';
}
showTabsMapLibreNote(tabsMapLibreMap.maplace.getSnapshot().selectedId);
tabsMapLibreMap.addEventListener('maplace-selectionchange', (event) => showTabsMapLibreNote(event.detail.id));
</script><!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<!-- The list sits outside the map, styled as tabs above it, starting on the first place; the chosen tab's details sit right below the tabs, above the map, and follow the selection. -->
<style>
#tabs-map { flex-direction: column; }
#tabs-map > [role="listbox"] { order: -2; display: flex; flex: none; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
#tabs-map > [role="listbox"] > [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
#tabs-map > [role="listbox"] > [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
#tabs-map > #tabs-details { order: -1; padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<maplace-map id="tabs-map" api-key="YOUR_KEY" list selected-id="london">
<maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London" zoom="6"></maplace-location>
<maplace-location location-id="edinburgh" lat="55.9533" lng="-3.1883" title="Edinburgh" zoom="6"></maplace-location>
<aside id="tabs-details"></aside>
</maplace-map>
<script>
const tabsNotes = { london: 'Capital of the United Kingdom.', edinburgh: 'Capital of Scotland, home of the Fringe arts festival.' };
const tabsMap = document.getElementById('tabs-map');
function showTabsNote(id) {
document.getElementById('tabs-details').textContent = tabsNotes[id] ?? '';
}
showTabsNote(tabsMap.maplace.getSnapshot().selectedId);
tabsMap.addEventListener('maplace-selectionchange', (event) => showTabsNote(event.detail.id));
</script><!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet, what the tabs, their details and the map mount on, and the map made with Maplace. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
#tabs-maplibre-script-panel { display: flex; flex-direction: column; height: 24rem; }
#tabs-maplibre-script-map { flex: 1 1 auto; min-height: 0; }
#tabs-maplibre-script-list [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
#tabs-maplibre-script-list [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
#tabs-maplibre-script-list [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
#tabs-maplibre-script-details { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-maplibre-script-panel">
<div id="tabs-maplibre-script-list"></div>
<aside id="tabs-maplibre-script-details"></aside>
<div id="tabs-maplibre-script-map"></div>
</div>
<script type="module">
const notes = {
london: "Capital of the United Kingdom.",
edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};
const maplace = Maplace.createMapLibreMap("#tabs-maplibre-script-map", {
selectedId: "london",
locations: [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
{ id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
],
list: { container: "#tabs-maplibre-script-list" },
});
const details = document.querySelector("#tabs-maplibre-script-details");
showNote(maplace.getSnapshot().selectedId);
maplace.on("selectionChange", ({ id }) => showNote(id));
/** The chosen tab's details, in the panel below the tabs. */
function showNote(id) {
if (details) details.textContent = id === undefined ? "" : (notes[id] ?? "");
}
</script><!doctype html>
<!-- A page of your own script: the CDN script, what the tabs, their details and the map mount on, and the map made with Maplace, which adds Maps JS with your key. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
#tabs-script-panel { display: flex; flex-direction: column; height: 24rem; }
#tabs-script-map { flex: 1 1 auto; min-height: 0; }
#tabs-script-list [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
#tabs-script-list [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
#tabs-script-list [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
#tabs-script-details { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-script-panel">
<div id="tabs-script-list"></div>
<aside id="tabs-script-details"></aside>
<div id="tabs-script-map"></div>
</div>
<script type="module">
const notes = {
london: "Capital of the United Kingdom.",
edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};
const maplace = Maplace.createGoogleMap("#tabs-script-map", {
apiKey: "YOUR_KEY",
selectedId: "london",
locations: [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
{ id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
],
list: { container: "#tabs-script-list" },
});
const details = document.querySelector("#tabs-script-details");
showNote(maplace.getSnapshot().selectedId);
maplace.on("selectionChange", ({ id }) => showNote(id));
/** The chosen tab's details, in the panel below the tabs. */
function showNote(id) {
if (details) details.textContent = id === undefined ? "" : (notes[id] ?? "");
}
</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>
#tabs-maplibre-vanilla-panel { display: flex; flex-direction: column; height: 24rem; }
#tabs-maplibre-vanilla-map { flex: 1 1 auto; min-height: 0; }
#tabs-maplibre-vanilla-list [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
#tabs-maplibre-vanilla-list [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
#tabs-maplibre-vanilla-list [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
#tabs-maplibre-vanilla-details { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-maplibre-vanilla-panel">
<div id="tabs-maplibre-vanilla-list"></div>
<aside id="tabs-maplibre-vanilla-details"></aside>
<div id="tabs-maplibre-vanilla-map"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import "maplibre-gl/dist/maplibre-gl.css";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";
const notes: Readonly<Record<string, string>> = {
london: "Capital of the United Kingdom.",
edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};
const maplace = createMapLibreMap("#tabs-maplibre-vanilla-map", {
selectedId: "london",
locations: [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
{ id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
],
list: { container: "#tabs-maplibre-vanilla-list" },
});
const details = document.querySelector("#tabs-maplibre-vanilla-details");
showNote(maplace.getSnapshot().selectedId);
maplace.on("selectionChange", ({ id }) => showNote(id));
/** The chosen tab's details, in the panel below the tabs. */
function showNote(id: string | undefined): void {
if (details) details.textContent = id === undefined ? "" : (notes[id] ?? "");
}<!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>
#tabs-vanilla-panel { display: flex; flex-direction: column; height: 24rem; }
#tabs-vanilla-map { flex: 1 1 auto; min-height: 0; }
#tabs-vanilla-list [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
#tabs-vanilla-list [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
#tabs-vanilla-list [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
#tabs-vanilla-details { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-vanilla-panel">
<div id="tabs-vanilla-list"></div>
<aside id="tabs-vanilla-details"></aside>
<div id="tabs-vanilla-map"></div>
</div>
<script type="module" src="./vanilla.ts"></script>import { createGoogleMap } from "@maplacejs/maplace/google";
const notes: Readonly<Record<string, string>> = {
london: "Capital of the United Kingdom.",
edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};
const maplace = createGoogleMap("#tabs-vanilla-map", {
selectedId: "london",
locations: [
{ id: "london", lat: 51.5074, lng: -0.1278, title: "London", zoom: 6 },
{ id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
],
list: { container: "#tabs-vanilla-list" },
});
const details = document.querySelector("#tabs-vanilla-details");
showNote(maplace.getSnapshot().selectedId);
maplace.on("selectionChange", ({ id }) => showNote(id));
/** The chosen tab's details, in the panel below the tabs. */
function showNote(id: string | undefined): void {
if (details) details.textContent = id === undefined ? "" : (notes[id] ?? "");
}<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#tabs-maplibre-react-root .panel { display: flex; flex-direction: column; height: 24rem; }
#tabs-maplibre-react-root .map { flex: 1 1 auto; min-height: 0; }
#tabs-maplibre-react-root [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
#tabs-maplibre-react-root [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
#tabs-maplibre-react-root [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
#tabs-maplibre-react-root aside { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { LocationList, 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", zoom: 6 },
{ id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
];
const notes: Readonly<Record<string, string>> = {
london: "Capital of the United Kingdom.",
edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};
const noteOf = (id: string | undefined) => (id === undefined ? "" : (notes[id] ?? ""));
/** The chosen place's note, read from the store: the selection it starts on, and every one after. */
function TabDetails() {
const maplace = useMaplace();
const selectedId = useSyncExternalStore(
maplace.subscribe,
() => maplace.getSnapshot().selectedId,
);
return <aside>{noteOf(selectedId)}</aside>;
}
function Tabs() {
return (
<MaplaceProvider locations={cities} defaultSelectedId="london">
<div className="panel">
<LocationList />
<TabDetails />
<MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("tabs-maplibre-react-root") as HTMLElement).render(
<StrictMode>
<Tabs />
</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>
#tabs-react-root .panel { display: flex; flex-direction: column; height: 24rem; }
#tabs-react-root .map { flex: 1 1 auto; min-height: 0; }
#tabs-react-root [role="listbox"] { display: flex; gap: 0.25rem; padding: 0.5rem 0.75rem 0; box-shadow: inset 0 -1px color-mix(in srgb, currentColor 25%, transparent); }
#tabs-react-root [role="option"] { padding: 0.5rem 1rem; border: 1px solid transparent; border-bottom: 0; border-radius: 0.5rem 0.5rem 0 0; }
#tabs-react-root [aria-selected="true"] { position: relative; border-color: color-mix(in srgb, currentColor 25%, transparent); background: Canvas; font-weight: 600; }
#tabs-react-root aside { padding: 0.75rem 1.25rem 1rem; line-height: 1.5; }
</style>
<div id="tabs-react-root"></div>
<script type="module" src="./react.tsx"></script>import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, 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", zoom: 6 },
{ id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", zoom: 6 },
];
const notes: Readonly<Record<string, string>> = {
london: "Capital of the United Kingdom.",
edinburgh: "Capital of Scotland, home of the Fringe arts festival.",
};
const noteOf = (id: string | undefined) => (id === undefined ? "" : (notes[id] ?? ""));
/** The chosen place's note, read from the store: the selection it starts on, and every one after. */
function TabDetails() {
const maplace = useMaplace();
const selectedId = useSyncExternalStore(
maplace.subscribe,
() => maplace.getSnapshot().selectedId,
);
return <aside>{noteOf(selectedId)}</aside>;
}
function Tabs() {
return (
<MaplaceProvider locations={cities} defaultSelectedId="london">
<div className="panel">
<LocationList />
<TabDetails />
<MaplaceMap createAdapter={createGoogleAdapter} className="map" />
</div>
</MaplaceProvider>
);
}
createRoot(document.getElementById("tabs-react-root") as HTMLElement).render(
<StrictMode>
<Tabs />
</StrictMode>,
);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:
listandselected-idon<maplace-map>(Map attributes), the page’s CSS drawing the list as tabs through itsrole="listbox"androle="option", and amaplace-selectionchangelistener filling the panel (Events). - Script and Bundler:
selectedIdin the map’s options andlist: { container }to mount the list on a node of yours (Options, List view options), and aselectionChangelistener (Events). - React:
defaultSelectedIdon<MaplaceProvider>,<LocationList />outside the<MaplaceMap>, and a component reading the selection withuseMaplace().