Map search added to global search. Preview cards are displayed for all results in new Map tab.
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run

Map viewer has simple search.

Map search databases can be downloaded from CDN, or built locally. Map searches can be built offline, but will not have enriched things like state names downloaded from Wikidata.
This commit is contained in:
Roland 2026-04-11 20:09:30 -06:00
parent aaa3c0aff7
commit c726654de9
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
21 changed files with 1831 additions and 62 deletions

View file

@ -0,0 +1,66 @@
<!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 PMTiles files (skip terrain). Includes map-overview if present.
var files = (data.files || []).filter(function(f) {
return f.name.indexOf("terrain") !== 0;
});
files.forEach(function(f, i) {
sources[f.name] = {type: "vector", url: "pmtiles:///data/" + f.name};
var layers = protomaps_themes_base.default(f.name, flavor);
if (i > 0) {
layers.forEach(function(l) { l.id = f.name + ":" + 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>

View file

@ -124,6 +124,43 @@
user-select: none;
}
#context-menu .action:hover { background: #f0f0f0; }
/* Search */
#search-container {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
z-index: 1000;
width: 320px;
max-width: calc(100% - 100px);
}
#search-input {
width: 100%;
padding: 8px 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
#search-results {
display: none;
background: white;
border: 1px solid #ccc;
border-top: none;
border-radius: 0 0 4px 4px;
max-height: 300px;
overflow-y: auto;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
.search-result {
padding: 8px 12px;
cursor: pointer;
border-bottom: 1px solid #eee;
}
.search-result:hover { background: #f0f0f0; }
.search-result-name { font-weight: 500; }
.search-result-kind { font-size: 11px; color: #888; }
</style>
</head>
<body>
@ -146,6 +183,11 @@
<div id="status"></div>
<div id="search-container">
<input type="text" id="search-input" placeholder="Search places...">
<div id="search-results"></div>
</div>
<div id="context-menu">
<div class="coords" id="context-coords"></div>
<div class="action" id="context-copy">Copy GPS coordinates</div>
@ -417,40 +459,39 @@
var longPressTimer = null;
var touchStartPos = null;
var canvas = map.getCanvas();
canvas.addEventListener("touchstart", function(e) {
if (e.touches.length !== 1) return;
touchStartPos = {x: e.touches[0].clientX, y: e.touches[0].clientY};
longPressTimer = setTimeout(function() {
var rect = canvas.getBoundingClientRect();
var px = touchStartPos.x - rect.left;
var py = touchStartPos.y - rect.top;
var lngLat = map.unproject([px, py]);
contextMenuData = {lat: lngLat.lat, lon: lngLat.lng};
var menu = document.getElementById("context-menu");
var z = map.getZoom();
var d = z < 4 ? 1 : z < 8 ? 2 : z < 12 ? 3 : 4;
document.getElementById("context-coords").textContent =
contextMenuData.lat.toFixed(d) + ", " + contextMenuData.lon.toFixed(d);
menu.style.left = px + "px";
menu.style.top = py + "px";
menu.style.display = "block";
}, 500);
}, {passive: true});
canvas.addEventListener("touchmove", function(e) {
if (longPressTimer && touchStartPos) {
var dx = e.touches[0].clientX - touchStartPos.x;
var dy = e.touches[0].clientY - touchStartPos.y;
if (Math.abs(dx) > 10 || Math.abs(dy) > 10) {
clearTimeout(longPressTimer);
longPressTimer = null;
canvas.addEventListener("touchstart", function(e) {
if (e.touches.length !== 1) return;
touchStartPos = {x: e.touches[0].clientX, y: e.touches[0].clientY};
longPressTimer = setTimeout(function() {
var rect = canvas.getBoundingClientRect();
var px = touchStartPos.x - rect.left;
var py = touchStartPos.y - rect.top;
var lngLat = map.unproject([px, py]);
contextMenuData = {lat: lngLat.lat, lon: lngLat.lng};
var menu = document.getElementById("context-menu");
var z = map.getZoom();
var d = z < 4 ? 1 : z < 8 ? 2 : z < 12 ? 3 : 4;
document.getElementById("context-coords").textContent =
contextMenuData.lat.toFixed(d) + ", " + contextMenuData.lon.toFixed(d);
menu.style.left = px + "px";
menu.style.top = py + "px";
menu.style.display = "block";
}, 500);
}, {passive: true});
canvas.addEventListener("touchmove", function(e) {
if (longPressTimer && touchStartPos) {
var dx = e.touches[0].clientX - touchStartPos.x;
var dy = e.touches[0].clientY - touchStartPos.y;
if (Math.abs(dx) > 10 || Math.abs(dy) > 10) {
clearTimeout(longPressTimer);
longPressTimer = null;
}
}
}
});
canvas.addEventListener("touchend", function() {
clearTimeout(longPressTimer);
longPressTimer = null;
});
});
canvas.addEventListener("touchend", function() {
clearTimeout(longPressTimer);
longPressTimer = null;
});
map.on("load", function() {
buildLayerControls();
});
@ -731,6 +772,117 @@
if (map) reloadMap();
});
// --- Search ---
var searchInput = document.getElementById("search-input");
var searchResults = document.getElementById("search-results");
var searchTimeout = null;
var searchMarker = null;
var coordRegex = /^\s*(-?\d+\.?\d*)\s*[,\s]\s*(-?\d+\.?\d*)\s*$/;
searchInput.addEventListener("input", function() {
clearTimeout(searchTimeout);
var query = this.value.trim();
if (query.length < 2) {
searchResults.style.display = "none";
return;
}
// Check for coordinate input.
var coordMatch = query.match(coordRegex);
if (coordMatch) {
var cLat = parseFloat(coordMatch[1]);
var cLon = parseFloat(coordMatch[2]);
if (cLat >= -90 && cLat <= 90 && cLon >= -180 && cLon <= 180) {
searchResults.innerHTML = "";
var div = document.createElement("div");
div.className = "search-result";
var nameEl = document.createElement("div");
nameEl.className = "search-result-name";
nameEl.textContent = cLat + ", " + cLon;
div.appendChild(nameEl);
var kindEl = document.createElement("div");
kindEl.className = "search-result-kind";
kindEl.textContent = "coordinates";
div.appendChild(kindEl);
div.onclick = function() { goToResult(cLat, cLon, cLat + ", " + cLon); };
searchResults.appendChild(div);
searchResults.style.display = "block";
return;
}
}
searchTimeout = setTimeout(function() {
var url = "/api/map/search?q=" + encodeURIComponent(query) + "&limit=8";
if (map) {
var center = map.getCenter();
url += "&lat=" + center.lat + "&lon=" + center.lng;
}
fetch(url).then(function(r) { return r.json(); }).then(function(data) {
searchResults.innerHTML = "";
var results = data.results || [];
if (results.length > 0) {
results.forEach(function(r) {
var div = document.createElement("div");
div.className = "search-result";
var parts = [];
if (r.region) parts.push(r.region);
var detail = r.kind_detail || r.kind || "";
if (detail && !r.region) parts.push(detail.replace(/_/g, " "));
if (r.population) {
var pop = r.population >= 1000000 ? (r.population / 1000000).toFixed(1) + "M"
: r.population >= 1000 ? Math.round(r.population / 1000) + "K"
: "" + r.population;
parts.push("pop. " + pop);
}
var subtitle = parts.join(" \u00b7 ");
var nameEl = document.createElement("div");
nameEl.className = "search-result-name";
nameEl.textContent = r.name;
div.appendChild(nameEl);
if (subtitle) {
var kindEl = document.createElement("div");
kindEl.className = "search-result-kind";
kindEl.textContent = subtitle;
div.appendChild(kindEl);
}
div.onclick = function() { goToResult(r.lat, r.lon, r.name); };
searchResults.appendChild(div);
});
searchResults.style.display = "block";
} else {
searchResults.style.display = "none";
}
}).catch(function() { searchResults.style.display = "none"; });
}, 300);
});
searchInput.addEventListener("keydown", function(e) {
if (e.key === "Escape") {
searchResults.style.display = "none";
searchInput.blur();
}
});
document.addEventListener("click", function(e) {
if (!document.getElementById("search-container").contains(e.target)) {
searchResults.style.display = "none";
}
});
function goToResult(lat, lon, name) {
if (!map) return;
if (searchMarker) { searchMarker.remove(); searchMarker = null; }
var zoom = Math.max(map.getZoom(), 12);
map.flyTo({center: [lon, lat], zoom: zoom});
searchMarker = new maplibregl.Marker({color: "#6435c9"})
.setLngLat([lon, lat])
.setPopup(new maplibregl.Popup({offset: 25}).setText(name))
.addTo(map);
searchMarker.togglePopup();
searchResults.style.display = "none";
searchInput.value = name;
}
// --- Start ---
scanFiles();
})();