mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 14:36:22 -05:00
Any PMTiles file under the map directory, at any depth, can be shown. The API reads each file's header and metadata and says what it is (basemap, terrain, overlay, unreadable). Overlays are offered in the layer control under the name the file gives itself, hidden until turned on, with a checkbox per vector layer, and are drawn generically. Map files are named by their path within the map directory everywhere, and unfinished downloads are no longer listed. A file can be ignored from the Manage page; ignored files are kept in a new maps.yaml config. PMTiles metadata is not decompressed past its size limit, and the standalone viewer encodes map file paths.
72 lines
2.7 KiB
HTML
72 lines
2.7 KiB
HTML
<!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>
|