fix(regions): keep map labels of nearby regions apart #171

Merged
Sebi merged 1 commits from fix/region-map-overlap into main 2026-11-29 10:10:23 +00:00
3 changed files with 74 additions and 1 deletions

No files matched your search

+16
View File
@@ -164,6 +164,22 @@
font-size: 12px;
}
}
/* Pins whose label would cover another one turn it to the left of their
point, or show only their dot until hovered. */
.is-flipped > .region-pin {
flex-direction: row-reverse;
padding: 3px 5px 3px 9px;
translate: calc(16px - 100%);
}
.maplibregl-marker:is(.is-flipped, .is-compact):not(.is-hover, :hover) { z-index: 2; }
.is-compact > .region-pin { gap: 0; padding: 3px 5px; }
.is-compact:not(.is-hover, :hover) > .region-pin > :last-child { display: none; }
.is-compact.is-hover > .region-pin,
.is-compact:hover > .region-pin { gap: 6px; padding: 3px 9px 3px 5px; }
@media (pointer: coarse) {
.is-flipped > .region-pin { padding: 6px 8px 6px 11px; }
.is-compact > .region-pin { padding: 6px 8px; }
}
.region-pin:focus-visible { outline: 2px solid var(--color-maintenance); outline-offset: 2px; }
.region-pick {
width: 14px;
+44
View File
@@ -698,6 +698,7 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
fit();
const selecting = box.dataset.regionMap === "select";
const markers = [];
for (const item of items) {
// The list row (a label or table row) and the marker light up together.
const row = item.closest("label, tr") ?? item;
@@ -708,6 +709,7 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
const holder = document.createElement("div");
holder.append(pin);
const marker = new gl.Marker({ element: holder, anchor: "left", offset: [-8, 0] }).setLngLat(lngLat(item)).addTo(map);
markers.push({ holder, pin, marker });
const hover = (on) => {
holder.classList.toggle("is-hover", on);
row.classList.toggle("is-hover", on);
@@ -743,6 +745,48 @@ for (const el of document.querySelectorAll("input[data-tz-default]")) {
});
}
}
// Labels run to the right of their point, or to the left where that
// would cover another pin or leave the map, as with regions a few
// kilometres apart. A pin with room on neither side shows only its dot
// and its name on hover; the list names every region. No label covers
// the dot of another pin.
const place = () => {
const width = box.clientWidth;
const height = box.clientHeight;
if (!width) return;
for (const m of markers) m.holder.classList.remove("is-flipped", "is-compact");
const spots = markers.map((m) => {
const at = map.project(m.marker.getLngLat());
const h = m.pin.offsetHeight;
return { m, at, w: m.pin.offsetWidth, h, dot: { left: at.x - 8, right: at.x + 12, top: at.y - h / 2, bottom: at.y + h / 2 } };
});
// The rounded ends of two pills may touch.
const hits = (a, b) => a.left + 4 < b.right - 4 && b.left + 4 < a.right - 4 && a.top < b.bottom && b.top < a.bottom;
const labels = [];
const fits = (a, self) =>
a.left >= 0 && a.right <= width && a.top >= 0 && a.bottom <= height &&
!labels.some((b) => hits(a, b)) &&
// Dots stacked on its own one stay beside it either way.
!spots.some((s) => s !== self && !hits(s.dot, self.dot) && hits(a, s.dot));
for (const s of spots.sort((a, b) => b.at.x - a.at.x)) {
const right = { ...s.dot, right: s.at.x - 8 + s.w };
const left = { ...s.dot, left: s.at.x + 8 - s.w, right: s.at.x + 8 };
if (fits(right, s)) {
labels.push(right);
} else if (fits(left, s)) {
s.m.holder.classList.add("is-flipped");
labels.push(left);
} else {
s.m.holder.classList.add("is-compact");
}
}
};
map.on("load", place);
map.on("zoom", place);
map.on("moveend", place);
map.on("resize", place);
if (selecting) return;
let pick;
map.on("click", (ev) => {
+14 -1
View File
@@ -20,6 +20,12 @@
<input name="latitude" data-number data-nullable data-location-field="latitude">
<input name="longitude" data-number data-nullable data-location-field="longitude">
</div></form>
<div id="near-map" data-region-map="edit" data-region-items="#near" data-map-server="stub" data-touch-hint="t" data-scroll-hint="{key} s" data-ctrl-key="Ctrl" hidden style="width:300px;height:200px;position:relative"></div>
<table><tbody id="near">
<tr data-lat="50" data-lon="8" data-name="Nuremberg" data-live="1"><td></td></tr>
<tr data-lat="50.05" data-lon="8.5" data-name="Bamberg" data-live="1"><td></td></tr>
<tr data-lat="55" data-lon="19" data-name="Gdansk" data-live="1"><td></td></tr>
</tbody></table>
<pre id="out"></pre>
<script>
const sent = [];
@@ -35,11 +41,13 @@ window.maplibregl = {
fitBounds(b) { this.bounds = b.points; }
jumpTo(o) { this.center = o.center; }
setStyle(s) { this.style = s; }
// 10 px per degree, 8°E 50°N at the middle of the box.
project({ lng, lat }) { return { x: 100 + (lng - 0) * 10, y: 100 - (lat - 50) * 10 }; }
},
Marker: class {
constructor(o) { this.el = o.element; }
setLngLat(p) { this.at = p; this.el.dataset.at = p.join(","); return this; }
getLngLat() { return this.at; }
getLngLat() { return { lng: this.at[0], lat: this.at[1] }; }
addTo(map) { map.container.append(this.el); return this; }
},
NavigationControl: class {},
@@ -100,6 +108,11 @@ window.maplibregl = {
click(1, 2);
check("closed row falls back to create form", create.elements.latitude.value, "1");
const near = maps[2];
near.fire("load");
const placement = () => [...near.container.querySelectorAll(".region-pin")].map((p) => [p.title, p.parentElement.classList.contains("is-flipped") ? "left" : p.parentElement.classList.contains("is-compact") ? "dot" : "right"]);
check("close labels make room", placement(), [["Nuremberg", "dot"], ["Bamberg", "right"], ["Gdansk", "left"]]);
document.getElementById("out").textContent = out.join("\n");
})();
</script>