Load the map on click
A panel in the map's place: the map loads only when the visitor presses its button.
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>
<!-- hold keeps the map idle, asking its provider for nothing, until the reader presses its Load map button. -->
<maplace-map id="load-on-demand-maplibre-map" hold>
<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-location location-id="naples" lat="40.8518" lng="14.2681" title="Naples"></maplace-location>
</maplace-map><!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<!-- hold keeps the map idle, asking its provider for nothing, until the reader presses its Load map button. -->
<maplace-map id="load-on-demand-map" api-key="YOUR_KEY" hold>
<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-location location-id="naples" lat="40.8518" lng="14.2681" title="Naples"></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>
#load-on-demand-maplibre-script-map { height: 24rem; }
/* The panel the map waits behind: the look <maplace-map hold> gives its own. */
#load-on-demand-maplibre-script-map .load-panel { display: grid; place-content: center; justify-items: center; gap: 0.75rem; height: 100%; padding-inline: 1rem; text-align: center; background: #8881; }
#load-on-demand-maplibre-script-map .load-panel > button { padding: 0.5rem 1rem; border: 1px solid; border-radius: 0.375rem; background: none; color: inherit; font: inherit; font-weight: 600; cursor: pointer; }
</style>
<div id="load-on-demand-maplibre-script-map"></div>
<script type="module">
const box = document.getElementById("load-on-demand-maplibre-script-map");
const locations = [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
{ id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
];
const panel = document.createElement("div");
panel.className = "load-panel";
const line = document.createElement("span");
line.textContent = "Loads from OpenFreeMap";
const button = document.createElement("button");
button.type = "button";
button.textContent = "Load map";
button.addEventListener("click", () => {
// The panel goes as it's pressed: focus stays here, not at the top of the page.
box.tabIndex = -1;
box.focus({ preventScroll: true });
panel.remove();
Maplace.createMapLibreMap(box, { locations });
});
panel.append(line, button);
box.append(panel);
</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>
#load-on-demand-script-map { height: 24rem; }
/* The panel the map waits behind: the look <maplace-map hold> gives its own. */
#load-on-demand-script-map .load-panel { display: grid; place-content: center; justify-items: center; gap: 0.75rem; height: 100%; padding-inline: 1rem; text-align: center; background: #8881; }
#load-on-demand-script-map .load-panel > button { padding: 0.5rem 1rem; border: 1px solid; border-radius: 0.375rem; background: none; color: inherit; font: inherit; font-weight: 600; cursor: pointer; }
</style>
<div id="load-on-demand-script-map"></div>
<script type="module">
const box = document.getElementById("load-on-demand-script-map");
const locations = [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
{ id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
];
const panel = document.createElement("div");
panel.className = "load-panel";
const line = document.createElement("span");
line.textContent = "Loads from Google";
const button = document.createElement("button");
button.type = "button";
button.textContent = "Load map";
button.addEventListener("click", () => {
// The panel goes as it's pressed: focus stays here, not at the top of the page.
box.tabIndex = -1;
box.focus({ preventScroll: true });
panel.remove();
Maplace.createGoogleMap(box, { apiKey: "YOUR_KEY", locations });
});
panel.append(line, button);
box.append(panel);
</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>
#load-on-demand-maplibre-vanilla-map { height: 24rem; }
/* The panel the map waits behind: the look <maplace-map hold> gives its own. */
#load-on-demand-maplibre-vanilla-map .load-panel { display: grid; place-content: center; justify-items: center; gap: 0.75rem; height: 100%; padding-inline: 1rem; text-align: center; background: #8881; }
#load-on-demand-maplibre-vanilla-map .load-panel > button { padding: 0.5rem 1rem; border: 1px solid; border-radius: 0.375rem; background: none; color: inherit; font: inherit; font-weight: 600; cursor: pointer; }
</style>
<div id="load-on-demand-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";
const box = document.getElementById("load-on-demand-maplibre-vanilla-map") as HTMLElement;
const locations: Location[] = [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
{ id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
];
const panel = document.createElement("div");
panel.className = "load-panel";
const line = document.createElement("span");
line.textContent = "Loads from OpenFreeMap";
const button = document.createElement("button");
button.type = "button";
button.textContent = "Load map";
button.addEventListener("click", () => {
// The panel goes as it's pressed: focus stays here, not at the top of the page.
box.tabIndex = -1;
box.focus({ preventScroll: true });
panel.remove();
createMapLibreMap(box, { locations });
});
panel.append(line, button);
box.append(panel);<!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>
#load-on-demand-vanilla-map { height: 24rem; }
/* The panel the map waits behind: the look <maplace-map hold> gives its own. */
#load-on-demand-vanilla-map .load-panel { display: grid; place-content: center; justify-items: center; gap: 0.75rem; height: 100%; padding-inline: 1rem; text-align: center; background: #8881; }
#load-on-demand-vanilla-map .load-panel > button { padding: 0.5rem 1rem; border: 1px solid; border-radius: 0.375rem; background: none; color: inherit; font: inherit; font-weight: 600; cursor: pointer; }
</style>
<div id="load-on-demand-vanilla-map"></div>
<script type="module" src="./vanilla.ts"></script>import type { Location } from "@maplacejs/maplace";
import { createGoogleMap } from "@maplacejs/maplace/google";
const box = document.getElementById("load-on-demand-vanilla-map") as HTMLElement;
const locations: Location[] = [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
{ id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
];
const panel = document.createElement("div");
panel.className = "load-panel";
const line = document.createElement("span");
line.textContent = "Loads from Google";
const button = document.createElement("button");
button.type = "button";
button.textContent = "Load map";
button.addEventListener("click", () => {
// The panel goes as it's pressed: focus stays here, not at the top of the page.
box.tabIndex = -1;
box.focus({ preventScroll: true });
panel.remove();
createGoogleMap(box, { locations });
});
panel.append(line, button);
box.append(panel);<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
#load-on-demand-maplibre-react-root .box { height: 24rem; }
/* The panel the map waits behind: the look <maplace-map hold> gives its own. */
#load-on-demand-maplibre-react-root .load-panel { display: grid; place-content: center; justify-items: center; gap: 0.75rem; height: 100%; padding-inline: 1rem; text-align: center; background: #8881; }
#load-on-demand-maplibre-react-root .load-panel > button { padding: 0.5rem 1rem; border: 1px solid; border-radius: 0.375rem; background: none; color: inherit; font: inherit; font-weight: 600; cursor: pointer; }
#load-on-demand-maplibre-react-root .map { height: 100%; }
</style>
<div id="load-on-demand-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, useRef, useState } from "react";
import { createRoot } from "react-dom/client";
const cities: Location[] = [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
{ id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
];
function LoadOnDemand() {
const [loaded, setLoaded] = useState(false);
const box = useRef<HTMLDivElement>(null);
const load = () => {
// The panel goes as it's pressed: focus stays here, not at the top of the page.
box.current?.focus({ preventScroll: true });
setLoaded(true);
};
return (
<div className="box" ref={box} tabIndex={-1}>
{loaded ? (
<MaplaceProvider locations={cities}>
<MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
</MaplaceProvider>
) : (
<div className="load-panel">
<span>Loads from OpenFreeMap</span>
<button type="button" onClick={load}>
Load map
</button>
</div>
)}
</div>
);
}
createRoot(document.getElementById("load-on-demand-maplibre-react-root") as HTMLElement).render(
<StrictMode>
<LoadOnDemand />
</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>
#load-on-demand-react-root .box { height: 24rem; }
/* The panel the map waits behind: the look <maplace-map hold> gives its own. */
#load-on-demand-react-root .load-panel { display: grid; place-content: center; justify-items: center; gap: 0.75rem; height: 100%; padding-inline: 1rem; text-align: center; background: #8881; }
#load-on-demand-react-root .load-panel > button { padding: 0.5rem 1rem; border: 1px solid; border-radius: 0.375rem; background: none; color: inherit; font: inherit; font-weight: 600; cursor: pointer; }
#load-on-demand-react-root .map { height: 100%; }
</style>
<div id="load-on-demand-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, useRef, useState } from "react";
import { createRoot } from "react-dom/client";
const cities: Location[] = [
{ id: "rome", lat: 41.9028, lng: 12.4964, title: "Rome" },
{ id: "milan", lat: 45.4642, lng: 9.19, title: "Milan" },
{ id: "naples", lat: 40.8518, lng: 14.2681, title: "Naples" },
];
function LoadOnDemand() {
const [loaded, setLoaded] = useState(false);
const box = useRef<HTMLDivElement>(null);
const load = () => {
// The panel goes as it's pressed: focus stays here, not at the top of the page.
box.current?.focus({ preventScroll: true });
setLoaded(true);
};
return (
<div className="box" ref={box} tabIndex={-1}>
{loaded ? (
<MaplaceProvider locations={cities}>
<MaplaceMap createAdapter={createGoogleAdapter} className="map" />
</MaplaceProvider>
) : (
<div className="load-panel">
<span>Loads from Google</span>
<button type="button" onClick={load}>
Load map
</button>
</div>
)}
</div>
);
}
createRoot(document.getElementById("load-on-demand-react-root") as HTMLElement).render(
<StrictMode>
<LoadOnDemand />
</StrictMode>,
);How it works
The map stays idle in its place, its tiles and its provider’s requests waiting, until the visitor presses a button: for a heavy map low on a page, or to ask first.
- HTML:
holdon<maplace-map>shows the panel; its button callsrelease(), which a script of yours can call too.load-labelandload-from-labelset its words. For a map that waits until it scrolls into view instead,loading="lazy"(Load states). - Script and Bundler: a panel of yours, a line naming whom the map loads from and a button, with
createGoogleMaporcreateMapLibreMapin the button’s click listener, so nothing is created until then (Creating a store). The panel goes with the press, focus moved to the map’s box first, so a keyboard reader isn’t sent back to the top of the page. - React: state for whether it was pressed, rendering the panel or the
<MaplaceProvider>and<MaplaceMap>, the press moving the focus (<MaplaceMap>).