# Editable polygon

A polygon to edit: drag a corner, and the edit is written back.

## How it works

A three-cornered park you can edit: each edit is written back to the store and announced, for your page to save.

- **HTML**: a `<script type="application/json">` inside `<maplace-map>` holding the polygon ([Data](/element/#data)), and a `maplace-locationchange` listener ([Events](/element/#events)).
- **Script** and **Bundler**: `type: "polygon"`, `path` and `editable` on a location ([Polygons](/locations/#polygons)), and a `locationChange` listener ([Events](/store/#events)).
- **React**: the same location, with `onLocationChange` on `<MaplaceProvider>` ([Event props](/react/#event-props)).

## Code

### HTML on MapLibre

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

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- An editable area; edits are written back and announced. -->
<maplace-map id="polygon-maplibre-map">
  <script type="application/json">
    [{ "id": "park", "type": "polygon", "title": "Hyde Park", "editable": true,
       "path": [{ "lat": 51.512, "lng": -0.175 }, { "lat": 51.513, "lng": -0.16 }, { "lat": 51.503, "lng": -0.155 }] }]
  </script>
</maplace-map>
<script>
  document.getElementById('polygon-maplibre-map').addEventListener('maplace-locationchange', (event) => save(event.detail.location));
</script>
```

### HTML on Google

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

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<!-- An editable area; edits are written back and announced. -->
<maplace-map id="polygon-map" api-key="YOUR_KEY">
  <script type="application/json">
    [{ "id": "park", "type": "polygon", "title": "Hyde Park", "editable": true,
       "path": [{ "lat": 51.512, "lng": -0.175 }, { "lat": 51.513, "lng": -0.16 }, { "lat": 51.503, "lng": -0.155 }] }]
  </script>
</maplace-map>
<script>
  document.getElementById('polygon-map').addEventListener('maplace-locationchange', (event) => save(event.detail.location));
</script>
```

### Script on MapLibre

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

```html
<!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>
  #polygon-maplibre-script-map { height: 24rem; }
</style>
<div id="polygon-maplibre-script-map"></div>
<script type="module">
  // save(location): your own code, writing an edited location back to your server.

  const locations = [
    {
      id: "park",
      type: "polygon",
      title: "Hyde Park",
      editable: true,
      path: [
        { lat: 51.512, lng: -0.175 },
        { lat: 51.513, lng: -0.16 },
        { lat: 51.503, lng: -0.155 },
      ],
    },
  ];
  const maplace = Maplace.createMapLibreMap("#polygon-maplibre-script-map", { locations });
  maplace.on("locationChange", ({ location }) => save(location));
</script>
```

### Script on Google

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

```html
<!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>
  #polygon-script-map { height: 24rem; }
</style>
<div id="polygon-script-map"></div>
<script type="module">
  // save(location): your own code, writing an edited location back to your server.

  const locations = [
    {
      id: "park",
      type: "polygon",
      title: "Hyde Park",
      editable: true,
      path: [
        { lat: 51.512, lng: -0.175 },
        { lat: 51.513, lng: -0.16 },
        { lat: 51.503, lng: -0.155 },
      ],
    },
  ];
  const maplace = Maplace.createGoogleMap("#polygon-script-map", { apiKey: "YOUR_KEY", locations });
  maplace.on("locationChange", ({ location }) => save(location));
</script>
```

### Bundler on MapLibre

`packages/maplace/examples/polygon/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>
  #polygon-maplibre-vanilla-map { height: 24rem; }
</style>
<div id="polygon-maplibre-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>
```

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

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

/** Your own code, writing an edited location back to your server. */
declare function save(location: Location): Promise<void>;

const locations: Location[] = [
  {
    id: "park",
    type: "polygon",
    title: "Hyde Park",
    editable: true,
    path: [
      { lat: 51.512, lng: -0.175 },
      { lat: 51.513, lng: -0.16 },
      { lat: 51.503, lng: -0.155 },
    ],
  },
];
const maplace = createMapLibreMap("#polygon-maplibre-vanilla-map", { locations });
maplace.on("locationChange", ({ location }) => save(location));
```

### Bundler on Google

`packages/maplace/examples/polygon/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>
  #polygon-vanilla-map { height: 24rem; }
</style>
<div id="polygon-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>
```

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

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

/** Your own code, writing an edited location back to your server. */
declare function save(location: Location): Promise<void>;

const locations: Location[] = [
  {
    id: "park",
    type: "polygon",
    title: "Hyde Park",
    editable: true,
    path: [
      { lat: 51.512, lng: -0.175 },
      { lat: 51.513, lng: -0.16 },
      { lat: 51.503, lng: -0.155 },
    ],
  },
];
const maplace = createGoogleMap("#polygon-vanilla-map", { locations });
maplace.on("locationChange", ({ location }) => save(location));
```

### React on MapLibre

`packages/maplace/examples/polygon/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>
  #polygon-maplibre-react-root .map { height: 24rem; }
</style>
<div id="polygon-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

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

```tsx
import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";

/** Your own code, writing an edited location back to your server. */
declare function save(location: Location): Promise<void>;

const park: Location[] = [
  {
    id: "park",
    type: "polygon",
    title: "Hyde Park",
    editable: true,
    path: [
      { lat: 51.512, lng: -0.175 },
      { lat: 51.513, lng: -0.16 },
      { lat: 51.503, lng: -0.155 },
    ],
  },
];

function Polygon() {
  return (
    <MaplaceProvider locations={park} onLocationChange={({ location }) => void save(location)}>
      <MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
    </MaplaceProvider>
  );
}

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

### React on Google

`packages/maplace/examples/polygon/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>
  #polygon-react-root .map { height: 24rem; }
</style>
<div id="polygon-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

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

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

/** Your own code, writing an edited location back to your server. */
declare function save(location: Location): Promise<void>;

const park: Location[] = [
  {
    id: "park",
    type: "polygon",
    title: "Hyde Park",
    editable: true,
    path: [
      { lat: 51.512, lng: -0.175 },
      { lat: 51.513, lng: -0.16 },
      { lat: 51.503, lng: -0.155 },
    ],
  },
];

function Polygon() {
  return (
    <MaplaceProvider locations={park} onLocationChange={({ location }) => void save(location)}>
      <MaplaceMap createAdapter={createGoogleAdapter} className="map" />
    </MaplaceProvider>
  );
}

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