A GDPR-Compliant Google Maps Embed
An Astro component that loads Google Maps only after consent, stores the decision in localStorage and builds Google and Apple Maps links from coordinates.
Reviewed by adrian
on
Same starting point as the YouTube embed: a plain <iframe src="google.com/maps/embed/…"> sends data to Google as soon as the page loads. IP address, browser fingerprint, cookies, all without consent. For a public website that is a GDPR problem.
What I needed:
- No request to Google before the click. No map, no tile, nothing.
- Real consent. Clicking "Show once" or "Agree and remember" is the consent (Art. 6(1)(a) GDPR).
- App links. Anyone who would rather open the place in Google Maps or Apple Maps can do so without the iframe.
Usage
src/components/GoogleMapsEmbed.astro needs only coordinates and a name:
<GoogleMapsEmbed
lat={7.8807048}
lng={98.3657287}
title="Naka Weekend Market, Phuket"
/>
The component builds the embed URL and all app links from the coordinates.
| Prop | Type | Required | Description |
|---|---|---|---|
lat |
number | yes (or src) |
Latitude |
lng |
number | yes (or src) |
Longitude |
title |
string | no | Place name (label and app links) |
src |
string | yes (or lat + lng) |
Embed URL built by hand |
href |
string | no | Overrides the Google Maps link |
zoom |
number | no | Zoom level (default 15) |
The template
<figure class="maps-embed">
{title && <figcaption class="maps-caption">{title}</figcaption>}
<div class="maps-consent" data-src={src} data-title={title ?? ''}>
<h3>{t(locale, 'maps.externalContent')}</h3>
<p>{t(locale, 'maps.description')}</p>
<p>{t(locale, 'maps.serviceName')}</p>
<div class="maps-consent-buttons">
<button data-remember="true">{t(locale, 'maps.loadMapRemember')}</button>
<button>{t(locale, 'maps.loadMap')}</button>
</div>
<p class="maps-consent-footer">
{t(locale, 'maps.consentFooter')}
<a href={privacyHref}>{t(locale, 'maps.privacyLink')}</a>.
</p>
</div>
<div class="maps-actions">
<a href={googleMapsHref}>Google Maps</a>
{appleMapsHref && <a href={appleMapsHref}>Apple Maps</a>}
</div>
</figure>
The maps-actions bar with the app links sits outside .maps-consent. It stays visible whether the map is loaded or not. (In the original the two links each had a pin emoji in front.)
The script
<script>
const CONSENT_KEY = 'gmaps-consent';
function buildIframe(src, title) {
const iframe = document.createElement('iframe');
iframe.src = src;
iframe.allow = 'fullscreen';
iframe.loading = 'lazy';
iframe.referrerPolicy = 'no-referrer-when-downgrade';
iframe.style.cssText = 'width:100%;aspect-ratio:4/3;border:0;display:block';
if (title) iframe.title = title;
return iframe;
}
const hasConsent = (() => {
try { return localStorage.getItem(CONSENT_KEY) === '1'; } catch { return false; }
})();
document.querySelectorAll('.maps-consent').forEach((container) => {
const { src, title } = container.dataset;
if (!src) return;
if (hasConsent) { container.replaceWith(buildIframe(src, title)); return; }
container.querySelectorAll('button').forEach((btn) => {
btn.addEventListener('click', () => {
if (btn.dataset.remember === 'true') {
try { localStorage.setItem(CONSENT_KEY, '1'); } catch {}
}
container.replaceWith(buildIframe(src, title));
});
});
});
</script>
Two differences from the YouTube script:
referrerPolicy = 'no-referrer-when-downgrade'instead ofstrict-origin-when-cross-origin. Google Maps needs the full referrer (protocol and domain), but does not fail on a missing path the way YouTube does.- A separate
CONSENT_KEY(gmaps-consent) next toyt-consent. These are different services, and consent to YouTube does not mean consent to Maps.
Coordinates
// Auto-build src from coordinates if not provided
if (!src && lat != null && lng != null) {
src = `https://maps.google.com/maps?q=${lat},${lng}&output=embed&zoom=${zoom}`;
}
// Apple Maps URL
const appleMapsHref = lat && lng
? `https://maps.apple.com/?ll=${lat},${lng}&q=${encodeURIComponent(title ?? '')}`
: title
? `https://maps.apple.com/?q=${encodeURIComponent(title)}`
: null;
Without lat and lng props, the component parses the coordinates from the src URL (format ?q=lat,lng).
Texts
New keys in src/i18n/ui.ts:
'maps.externalContent': 'External content',
'maps.description': 'This is where you\'ll find an interactive Google Maps map.',
'maps.serviceName': 'Google Maps (maps.google.com)',
'maps.loadMap': 'Show once',
'maps.loadMapRemember': 'Agree and remember',
'maps.openInMaps': 'Open in Google Maps',
'maps.consentFooter': 'I agree to external content being displayed…',
'maps.privacyLink': 'privacy policy',
Adjusting the CSP
Google Maps needs two more entries in frame-src of the Caddy CSP:
- frame-src https://www.youtube-nocookie.com;
+ frame-src https://www.youtube-nocookie.com https://maps.google.com https://www.google.com;
How it differs from the YouTube embed
- No privacy mode. There is no counterpart to
youtube-nocookie.comfor Maps. The consent gate is the only protection before the embed loads. - App links always visible. Google Maps and Apple Maps work as a fallback without the iframe.
- Coordinates first.
latandlngbuild everything, so there is no embed URL to put together by hand.
The post ended with a live demo of Naka Weekend Market in Phuket: the consent box with its two buttons and, below it, the two app links.
Based on
-
Embedding Google Maps GDPR-compliant: consent box with app links
(
raw/2026/2026-04-25/embedding-google-maps-gdpr-compliant-consent-box-with-app-links.md) [post]
Topic
- Static Website A website made of finished HTML files that are built once and then served unchanged. No server code, no database.
- Web Development How this site and others are made and found: static pages, search engines, the IndieWeb and maps in the browser.
Builds on
- A GDPR-Compliant YouTube Embed An Astro component that loads YouTube only after a click: show once, agree and remember, or just the link. No cookie, no request to Google before consent.
Uses
- Astro Tool for building websites from content such as Markdown. Ships plain HTML by default and only loads JavaScript where it is needed.
- Caddy Web server and reverse proxy that obtains and renews TLS certificates on its own, configured in a short Caddyfile.
Mentioned
- Security Headers in Caddy A strict Content Security Policy, HSTS and no-referrer in a Caddy site block, checked with Webbkoll, securityheaders.com and the Mozilla Observatory.