# Dropdown of places

A native select on the map, grouped by region, with an entry for all.

## How it works

A native `<select>` on the map, top right, its places under their regions and a first entry that shows them all. Without `list-position` (or with the list mounted on a node of yours, or `<LocationList>` outside the `<MaplaceMap>`), the same menu sits beside the map instead.

- **HTML**: `list="select"`, `list-position`, `list-heading`, `fit-all` and `fit-all-label` on `<maplace-map>` ([Map attributes](/element/#map-attributes)), `group` on each `<maplace-location>` for its heading, and a script calling the element's `maplace.setFilter`.
- **Script** and **Bundler**: `list: { as: "select", heading, fitAll: true, fitAllLabel }` in the map's options ([List view options](/store/#list-view-options)), `group` on a location ([Every location](/locations/#every-location)), and [`setFilter(["North"])`](/store/#methods), a list of groups.
- **React**: `<LocationList as="select" heading fitAll fitAllLabel position="top-right" />` inside the `<MaplaceMap>` ([`<LocationList>`](/react/#locationlist)), and the `filter` prop of `<MaplaceProvider>` ([`<MaplaceProvider>`](/react/#maplaceprovider)).

## Code

### HTML on MapLibre

`packages/maplace/examples/menu/maplibre/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- A native <select> on the map, top right, grouped by region; the checkbox shows the North only. -->
<p><label><input type="checkbox" id="menu-maplibre-north-only" /> North only</label></p>
<maplace-map id="menu-maplibre-map" list="select" list-position="top-right"
  list-heading="Choose a location:" fit-all fit-all-label="All cities">
  <maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London" group="South"></maplace-location>
  <maplace-location location-id="edinburgh" lat="55.9533" lng="-3.1883" title="Edinburgh" group="North"></maplace-location>
  <maplace-location location-id="belfast" lat="54.5973" lng="-5.9301" title="Belfast" group="North"></maplace-location>
</maplace-map>
<script>
  document.getElementById('menu-maplibre-north-only').addEventListener('change', (event) => {
    document.getElementById('menu-maplibre-map').maplace.setFilter(event.target.checked ? ['North'] : undefined);
  });
</script>
```

### HTML on Google

`packages/maplace/examples/menu/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<!-- A native <select> on the map, top right, grouped by region; the checkbox shows the North only. -->
<p><label><input type="checkbox" id="menu-north-only" /> North only</label></p>
<maplace-map id="menu-map" api-key="YOUR_KEY" list="select" list-position="top-right"
  list-heading="Choose a location:" fit-all fit-all-label="All cities">
  <maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London" group="South"></maplace-location>
  <maplace-location location-id="edinburgh" lat="55.9533" lng="-3.1883" title="Edinburgh" group="North"></maplace-location>
  <maplace-location location-id="belfast" lat="54.5973" lng="-5.9301" title="Belfast" group="North"></maplace-location>
</maplace-map>
<script>
  document.getElementById('menu-north-only').addEventListener('change', (event) => {
    document.getElementById('menu-map').maplace.setFilter(event.target.checked ? ['North'] : undefined);
  });
</script>
```

### Script on MapLibre

`packages/maplace/examples/menu/maplibre/script.html`

```html
<!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet, the checkbox and 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>
  #menu-maplibre-script-map { height: 24rem; }
</style>
<p><label><input type="checkbox" id="menu-maplibre-script-north-only" /> North only</label></p>
<div id="menu-maplibre-script-map"></div>
<script type="module">
  const maplace = Maplace.createMapLibreMap("#menu-maplibre-script-map", {
    locations: [
      { id: "london", lat: 51.5074, lng: -0.1278, title: "London", group: "South" },
      { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", group: "North" },
      { id: "belfast", lat: 54.5973, lng: -5.9301, title: "Belfast", group: "North" },
    ],
    list: { as: "select", heading: "Choose a location:", fitAll: true, fitAllLabel: "All cities" },
  });

  const northOnly = document.querySelector("#menu-maplibre-script-north-only");
  northOnly?.addEventListener("change", () => {
    maplace.setFilter(northOnly.checked ? ["North"] : undefined);
  });
</script>
```

### Script on Google

`packages/maplace/examples/menu/script.html`

```html
<!doctype html>
<!-- A page of your own script: the CDN script, the checkbox and 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>
  #menu-script-map { height: 24rem; }
</style>
<p><label><input type="checkbox" id="menu-script-north-only" /> North only</label></p>
<div id="menu-script-map"></div>
<script type="module">
  const maplace = Maplace.createGoogleMap("#menu-script-map", {
    apiKey: "YOUR_KEY",
    locations: [
      { id: "london", lat: 51.5074, lng: -0.1278, title: "London", group: "South" },
      { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", group: "North" },
      { id: "belfast", lat: 54.5973, lng: -5.9301, title: "Belfast", group: "North" },
    ],
    list: { as: "select", heading: "Choose a location:", fitAll: true, fitAllLabel: "All cities" },
  });

  const northOnly = document.querySelector("#menu-script-north-only");
  northOnly?.addEventListener("change", () => {
    maplace.setFilter(northOnly.checked ? ["North"] : undefined);
  });
</script>
```

### Bundler on MapLibre

`packages/maplace/examples/menu/maplibre/vanilla.html`

```html
<!doctype html>
<!-- The page vanilla.ts runs on: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
  #menu-maplibre-vanilla-map { height: 24rem; }
</style>
<p><label><input type="checkbox" id="menu-maplibre-vanilla-north-only" /> North only</label></p>
<div id="menu-maplibre-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/menu/maplibre/vanilla.ts`

```ts
import "maplibre-gl/dist/maplibre-gl.css";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";

const maplace = createMapLibreMap("#menu-maplibre-vanilla-map", {
  locations: [
    { id: "london", lat: 51.5074, lng: -0.1278, title: "London", group: "South" },
    { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", group: "North" },
    { id: "belfast", lat: 54.5973, lng: -5.9301, title: "Belfast", group: "North" },
  ],
  list: { as: "select", heading: "Choose a location:", fitAll: true, fitAllLabel: "All cities" },
});

const northOnly = document.querySelector<HTMLInputElement>("#menu-maplibre-vanilla-north-only");
northOnly?.addEventListener("change", () => {
  maplace.setFilter(northOnly.checked ? ["North"] : undefined);
});
```

### Bundler on Google

`packages/maplace/examples/menu/vanilla.html`

```html
<!doctype html>
<!-- The page vanilla.ts runs on: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->
<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>
  #menu-vanilla-map { height: 24rem; }
</style>
<p><label><input type="checkbox" id="menu-vanilla-north-only" /> North only</label></p>
<div id="menu-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/menu/vanilla.ts`

```ts
import { createGoogleMap } from "@maplacejs/maplace/google";

const maplace = createGoogleMap("#menu-vanilla-map", {
  locations: [
    { id: "london", lat: 51.5074, lng: -0.1278, title: "London", group: "South" },
    { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", group: "North" },
    { id: "belfast", lat: 54.5973, lng: -5.9301, title: "Belfast", group: "North" },
  ],
  list: { as: "select", heading: "Choose a location:", fitAll: true, fitAllLabel: "All cities" },
});

const northOnly = document.querySelector<HTMLInputElement>("#menu-vanilla-north-only");
northOnly?.addEventListener("change", () => {
  maplace.setFilter(northOnly.checked ? ["North"] : undefined);
});
```

### React on MapLibre

`packages/maplace/examples/menu/maplibre/react.html`

```html
<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
  #menu-maplibre-react-root .map { height: 24rem; }
</style>
<div id="menu-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/menu/maplibre/react.tsx`

```tsx
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, useState } from "react";
import { createRoot } from "react-dom/client";

const cities: Location[] = [
  { id: "london", lat: 51.5074, lng: -0.1278, title: "London", group: "South" },
  { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", group: "North" },
  { id: "belfast", lat: 54.5973, lng: -5.9301, title: "Belfast", group: "North" },
];
const NORTH = ["North"];

function Menu() {
  const [northOnly, setNorthOnly] = useState(false);
  return (
    <MaplaceProvider locations={cities} filter={northOnly ? NORTH : undefined}>
      <p>
        <label>
          <input
            type="checkbox"
            checked={northOnly}
            onChange={(event) => setNorthOnly(event.target.checked)}
          />{" "}
          North only
        </label>
      </p>
      <MaplaceMap createAdapter={createMapLibreAdapter} className="map">
        <LocationList
          as="select"
          heading="Choose a location:"
          fitAll
          fitAllLabel="All cities"
          position="top-right"
        />
      </MaplaceMap>
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("menu-maplibre-react-root") as HTMLElement).render(
  <StrictMode>
    <Menu />
  </StrictMode>,
);
```

### React on Google

`packages/maplace/examples/menu/react.html`

```html
<!doctype html>
<!-- The page react.tsx renders into: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->
<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>
  #menu-react-root .map { height: 24rem; }
</style>
<div id="menu-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/menu/react.tsx`

```tsx
import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useState } from "react";
import { createRoot } from "react-dom/client";

const cities: Location[] = [
  { id: "london", lat: 51.5074, lng: -0.1278, title: "London", group: "South" },
  { id: "edinburgh", lat: 55.9533, lng: -3.1883, title: "Edinburgh", group: "North" },
  { id: "belfast", lat: 54.5973, lng: -5.9301, title: "Belfast", group: "North" },
];
const NORTH = ["North"];

function Menu() {
  const [northOnly, setNorthOnly] = useState(false);
  return (
    <MaplaceProvider locations={cities} filter={northOnly ? NORTH : undefined}>
      <p>
        <label>
          <input
            type="checkbox"
            checked={northOnly}
            onChange={(event) => setNorthOnly(event.target.checked)}
          />{" "}
          North only
        </label>
      </p>
      <MaplaceMap createAdapter={createGoogleAdapter} className="map">
        <LocationList
          as="select"
          heading="Choose a location:"
          fitAll
          fitAllLabel="All cities"
          position="top-right"
        />
      </MaplaceMap>
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("menu-react-root") as HTMLElement).render(
  <StrictMode>
    <Menu />
  </StrictMode>,
);
```
