wrolpi/modules/map/static/embed.html

73 lines
2.7 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/libs/maplibre-gl.css"/>
<style>* { margin: 0; padding: 0; } html, body, #map { width: 100%; height: 100%; }</style>
</head>
<body>
<div id="map"></div>
<script src="/libs/pmtiles.js"></script>
<script src="/libs/maplibre-gl.js"></script>
<script src="/libs/protomaps-themes-base.js"></script>
<script>
(function() {
var protocol = new pmtiles.Protocol();
maplibregl.addProtocol("pmtiles", protocol.tile);
var hash = window.location.hash.replace("#", "");
var parts = hash ? hash.split(",") : [];
var lat = parseFloat(parts[0]) || 0;
var lon = parseFloat(parts[1]) || 0;
var zoom = parseFloat(parts[2]) || 4;
var dark = window.matchMedia("(prefers-color-scheme: dark)").matches;
var flavor = dark ? "dark" : "light";
fetch("/api/map/files").then(function(r) { return r.json(); }).then(function(data) {
var sources = {};
var allLayers = [];
// Add user's basemap PMTiles files: not terrain, overlays, or files the user ignores.
var files = (data.files || []).filter(function(f) {
if (f.visible === false) return false;
return f.kind ? f.kind === "basemap" : f.name.indexOf("terrain") !== 0;
});
files.forEach(function(f, i) {
// A file is named by its path in the map directory; it may be in a sub-directory.
var mapPath = f.map_path || f.name;
// Each part of the path is encoded: a `#` or `?` in a file name would otherwise end the
// path, and another file (or none) would be asked for.
var dataPath = "/data/" + String(mapPath).split("/").map(encodeURIComponent).join("/");
sources[mapPath] = {type: "vector", url: "pmtiles://" + dataPath};
var layers = protomaps_themes_base.default(mapPath, flavor);
if (i > 0) {
layers.forEach(function(l) { l.id = mapPath + ":" + l.id; });
}
allLayers = allLayers.concat(layers);
});
new maplibregl.Map({
container: "map",
style: {
version: 8,
glyphs: "/fonts/{fontstack}/{range}.pbf",
sprite: window.location.origin + "/sprites/" + flavor,
sources: sources,
layers: allLayers,
},
center: [lon, lat],
zoom: zoom,
attributionControl: false,
interactive: false,
});
}).catch(function(e) {
console.error("Embed map error:", e);
document.getElementById("map").textContent = "Map unavailable";
});
})();
</script>
</body>
</html>