Editable polygon
A polygon to edit: drag a corner, and the edit is written back.
Map and code
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
Loading the map…
The map couldn't load.
These maps come from Google Maps and OpenFreeMap, which see your IP address. They stay off until you say so, once for the whole site. What loading the maps sends
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- 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><!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><!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><!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><!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>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));<!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>
#polygon-vanilla-map { height: 24rem; }
</style>
<div id="polygon-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>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));<!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>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>,
);<!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>
#polygon-react-root .map { height: 24rem; }
</style>
<div id="polygon-react-root"></div>
<script type="module" src="./react.tsx"></script>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>,
);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), and amaplace-locationchangelistener (Events). - Script and Bundler:
type: "polygon",pathandeditableon a location (Polygons), and alocationChangelistener (Events). - React: the same location, with
onLocationChangeon<MaplaceProvider>(Event props).