Suche nach Strassen und Hausnummern
This commit is contained in:
1 parent
a16f28b040
commit
518248ca00
12 files changed
+6346
-14
No files matched your search
@@ -50,6 +50,10 @@ const dispatcherState = {
|
||||
chatGroupId: null,
|
||||
chatByGroup: new Map(),
|
||||
chatKeysByGroup: new Map(),
|
||||
streetSearchResults: [],
|
||||
streetSearchActiveIndex: -1,
|
||||
streetSearchRequestId: 0,
|
||||
houseNumberMarker: null,
|
||||
unloading: false
|
||||
};
|
||||
|
||||
@@ -1021,7 +1025,10 @@ function handleMapClick(event) {
|
||||
actor: currentAccountId
|
||||
})
|
||||
});
|
||||
if (!sent) return;
|
||||
if (!sent) {
|
||||
cancelCrosshair();
|
||||
return;
|
||||
}
|
||||
|
||||
group.last_lat = lat;
|
||||
group.last_lon = lon;
|
||||
@@ -1030,6 +1037,7 @@ function handleMapClick(event) {
|
||||
|
||||
updateMarkerPosition(group);
|
||||
refreshGroupControls();
|
||||
cancelCrosshair();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
@@ -1232,6 +1240,361 @@ function zoomToStreet(streetId) {
|
||||
map.fitBounds(bounds, {padding: [30, 30], maxZoom: 18});
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Straßensuche und Hausnummern */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
const STREET_SEARCH_MIN_CHARS = 2;
|
||||
const STREET_SEARCH_MAX_RESULTS = 8;
|
||||
|
||||
function normalizeStreetSearchText(value) {
|
||||
return String(value || '')
|
||||
.trim()
|
||||
.toLocaleLowerCase('de-DE')
|
||||
.replace(/ß/g, 'ss')
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/\s+/g, ' ');
|
||||
}
|
||||
|
||||
function findStreetBySearchName(value) {
|
||||
const normalized = normalizeStreetSearchText(value);
|
||||
if (!normalized) return null;
|
||||
|
||||
return dispatcherState.streets.find(street => (
|
||||
normalizeStreetSearchText(street.name) === normalized
|
||||
)) || null;
|
||||
}
|
||||
|
||||
function splitStreetAndHouseNumber(value) {
|
||||
const text = String(value || '').trim();
|
||||
const match = text.match(/^(.*\S)\s+([0-9][0-9A-Za-zÄÖÜäöüß.\/-]*)$/u);
|
||||
if (!match) return {streetText: text, houseNumber: null};
|
||||
|
||||
return {
|
||||
streetText: match[1].trim(),
|
||||
houseNumber: match[2]
|
||||
};
|
||||
}
|
||||
|
||||
function parseStreetSearch(value) {
|
||||
const text = String(value || '').trim();
|
||||
if (!text) {
|
||||
return {text: '', streetText: '', houseNumber: null, street: null};
|
||||
}
|
||||
|
||||
// Ein vollständiger Straßenname hat Vorrang, auch wenn er auf eine Zahl endet.
|
||||
const exactStreet = findStreetBySearchName(text);
|
||||
if (exactStreet) {
|
||||
return {
|
||||
text,
|
||||
streetText: exactStreet.name,
|
||||
houseNumber: null,
|
||||
street: exactStreet
|
||||
};
|
||||
}
|
||||
|
||||
const split = splitStreetAndHouseNumber(text);
|
||||
if (split.houseNumber) {
|
||||
const street = findStreetBySearchName(split.streetText);
|
||||
return {
|
||||
text,
|
||||
streetText: street ? street.name : split.streetText,
|
||||
houseNumber: split.houseNumber,
|
||||
street
|
||||
};
|
||||
}
|
||||
|
||||
return {text, streetText: text, houseNumber: null, street: null};
|
||||
}
|
||||
|
||||
function getStreetSearchResults(streetText) {
|
||||
const normalized = normalizeStreetSearchText(streetText);
|
||||
if (normalized.length < STREET_SEARCH_MIN_CHARS) return [];
|
||||
|
||||
return dispatcherState.streets
|
||||
.filter(street => normalizeStreetSearchText(street.name).startsWith(normalized))
|
||||
.sort((a, b) => String(a.name).localeCompare(String(b.name), 'de'))
|
||||
.slice(0, STREET_SEARCH_MAX_RESULTS);
|
||||
}
|
||||
|
||||
function setStreetSearchStatus(text, isError = false) {
|
||||
const element = document.getElementById('streetSearchStatus');
|
||||
if (!element) return;
|
||||
|
||||
element.textContent = text || '';
|
||||
element.classList.toggle('error', Boolean(text) && isError);
|
||||
}
|
||||
|
||||
function closeStreetSearchResults() {
|
||||
const list = document.getElementById('streetSearchResults');
|
||||
const input = document.getElementById('streetSearchInput');
|
||||
|
||||
if (list) {
|
||||
list.hidden = true;
|
||||
list.innerHTML = '';
|
||||
}
|
||||
if (input) {
|
||||
input.setAttribute('aria-expanded', 'false');
|
||||
input.removeAttribute('aria-activedescendant');
|
||||
}
|
||||
|
||||
dispatcherState.streetSearchResults = [];
|
||||
dispatcherState.streetSearchActiveIndex = -1;
|
||||
}
|
||||
|
||||
function setActiveStreetSearchIndex(index) {
|
||||
const list = document.getElementById('streetSearchResults');
|
||||
const input = document.getElementById('streetSearchInput');
|
||||
const results = dispatcherState.streetSearchResults;
|
||||
if (!list || !input || !results.length) return;
|
||||
|
||||
const normalizedIndex = ((index % results.length) + results.length) % results.length;
|
||||
dispatcherState.streetSearchActiveIndex = normalizedIndex;
|
||||
|
||||
list.querySelectorAll('.street-search-option').forEach((option, optionIndex) => {
|
||||
const active = optionIndex === normalizedIndex;
|
||||
option.classList.toggle('is-active', active);
|
||||
option.setAttribute('aria-selected', active ? 'true' : 'false');
|
||||
});
|
||||
|
||||
const activeOption = list.querySelector(
|
||||
`.street-search-option[data-index="${normalizedIndex}"]`
|
||||
);
|
||||
if (activeOption) {
|
||||
input.setAttribute('aria-activedescendant', activeOption.id);
|
||||
activeOption.scrollIntoView?.({block: 'nearest'});
|
||||
}
|
||||
}
|
||||
|
||||
function renderStreetSearchResults(results) {
|
||||
const list = document.getElementById('streetSearchResults');
|
||||
const input = document.getElementById('streetSearchInput');
|
||||
if (!list || !input) return;
|
||||
|
||||
if (!results.length) {
|
||||
closeStreetSearchResults();
|
||||
return;
|
||||
}
|
||||
|
||||
dispatcherState.streetSearchResults = results;
|
||||
dispatcherState.streetSearchActiveIndex = -1;
|
||||
list.innerHTML = '';
|
||||
|
||||
results.forEach((street, index) => {
|
||||
const option = document.createElement('li');
|
||||
option.id = `streetSearchOption${index}`;
|
||||
option.className = 'street-search-option';
|
||||
option.dataset.index = String(index);
|
||||
option.setAttribute('role', 'option');
|
||||
option.setAttribute('aria-selected', 'false');
|
||||
option.textContent = street.name;
|
||||
|
||||
// Fokus im Eingabefeld halten, damit der Klick nicht vom blur-
|
||||
// Handler verschluckt wird.
|
||||
option.addEventListener('mousedown', event => event.preventDefault());
|
||||
option.addEventListener('mouseenter', () => setActiveStreetSearchIndex(index));
|
||||
option.addEventListener('click', () => selectStreetSearchResult(street));
|
||||
|
||||
list.appendChild(option);
|
||||
});
|
||||
|
||||
list.hidden = false;
|
||||
input.setAttribute('aria-expanded', 'true');
|
||||
setActiveStreetSearchIndex(0);
|
||||
}
|
||||
|
||||
function updateStreetSearchSuggestions() {
|
||||
const input = document.getElementById('streetSearchInput');
|
||||
if (!input) return;
|
||||
|
||||
const parsed = parseStreetSearch(input.value);
|
||||
if (parsed.streetText.length < STREET_SEARCH_MIN_CHARS) {
|
||||
closeStreetSearchResults();
|
||||
return;
|
||||
}
|
||||
|
||||
const results = parsed.street
|
||||
? [parsed.street]
|
||||
: getStreetSearchResults(parsed.streetText);
|
||||
renderStreetSearchResults(results);
|
||||
}
|
||||
|
||||
function handleStreetSearchInput() {
|
||||
setStreetSearchStatus('');
|
||||
updateStreetSearchSuggestions();
|
||||
}
|
||||
|
||||
function handleStreetSearchKeydown(event) {
|
||||
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
||||
if (!dispatcherState.streetSearchResults.length) {
|
||||
updateStreetSearchSuggestions();
|
||||
}
|
||||
if (!dispatcherState.streetSearchResults.length) return;
|
||||
|
||||
event.preventDefault();
|
||||
const direction = event.key === 'ArrowDown' ? 1 : -1;
|
||||
setActiveStreetSearchIndex(
|
||||
dispatcherState.streetSearchActiveIndex + direction
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.key === 'Escape') {
|
||||
closeStreetSearchResults();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.key !== 'Enter') return;
|
||||
event.preventDefault();
|
||||
|
||||
const input = document.getElementById('streetSearchInput');
|
||||
const parsed = parseStreetSearch(input.value);
|
||||
|
||||
if (dispatcherState.streetSearchResults.length
|
||||
&& dispatcherState.streetSearchActiveIndex >= 0) {
|
||||
selectStreetSearchResult(
|
||||
dispatcherState.streetSearchResults[
|
||||
dispatcherState.streetSearchActiveIndex
|
||||
]
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (parsed.street) {
|
||||
performStreetSearch(input.value, parsed.street);
|
||||
return;
|
||||
}
|
||||
|
||||
const results = getStreetSearchResults(parsed.streetText);
|
||||
if (results.length) {
|
||||
selectStreetSearchResult(results[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
closeStreetSearchResults();
|
||||
setStreetSearchStatus('Keine Straße gefunden.', true);
|
||||
}
|
||||
|
||||
function selectStreetSearchResult(street) {
|
||||
const input = document.getElementById('streetSearchInput');
|
||||
const parsed = parseStreetSearch(input.value);
|
||||
const houseNumber = parsed.houseNumber;
|
||||
|
||||
input.value = houseNumber ? `${street.name} ${houseNumber}` : street.name;
|
||||
closeStreetSearchResults();
|
||||
performStreetSearch(input.value, street);
|
||||
}
|
||||
|
||||
async function performStreetSearch(value, selectedStreet = null) {
|
||||
const input = document.getElementById('streetSearchInput');
|
||||
const text = String(value ?? input.value ?? '').trim();
|
||||
const parsed = parseStreetSearch(text);
|
||||
|
||||
let street = selectedStreet || parsed.street;
|
||||
if (!street) {
|
||||
const results = getStreetSearchResults(parsed.streetText);
|
||||
street = results[0] || null;
|
||||
}
|
||||
|
||||
if (!street) {
|
||||
closeStreetSearchResults();
|
||||
setStreetSearchStatus('Keine Straße gefunden.', true);
|
||||
return;
|
||||
}
|
||||
|
||||
const houseNumber = parsed.houseNumber;
|
||||
const requestId = ++dispatcherState.streetSearchRequestId;
|
||||
|
||||
clearHouseNumberMarker();
|
||||
closeStreetSearchResults();
|
||||
setStreetSearchStatus('');
|
||||
input.value = houseNumber ? `${street.name} ${houseNumber}` : street.name;
|
||||
zoomToStreet(street.id);
|
||||
|
||||
if (!houseNumber) return;
|
||||
|
||||
setStreetSearchStatus(`Hausnummer ${houseNumber} wird gesucht …`);
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
street: street.name,
|
||||
house_number: houseNumber
|
||||
});
|
||||
const data = await fetchJson(`${apiBase}/init/hausnummern?${params.toString()}`);
|
||||
if (requestId !== dispatcherState.streetSearchRequestId) return;
|
||||
|
||||
const latitude = Number(data.latitude);
|
||||
const longitude = Number(data.longitude);
|
||||
if (!data.found || !Number.isFinite(latitude) || !Number.isFinite(longitude)) {
|
||||
setStreetSearchStatus(
|
||||
`Für Hausnummer ${houseNumber} ist keine Koordinate hinterlegt.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setHouseNumberMarker(
|
||||
[latitude, longitude],
|
||||
`${street.name} ${houseNumber}`
|
||||
);
|
||||
setStreetSearchStatus(`Hausnummer ${houseNumber} markiert.`);
|
||||
} catch (error) {
|
||||
if (requestId !== dispatcherState.streetSearchRequestId) return;
|
||||
console.error('Hausnummer konnte nicht geladen werden:', error);
|
||||
setStreetSearchStatus('Hausnummer konnte nicht geladen werden.', true);
|
||||
}
|
||||
}
|
||||
|
||||
function clearHouseNumberMarker() {
|
||||
if (!dispatcherState.houseNumberMarker) return;
|
||||
|
||||
map.removeLayer(dispatcherState.houseNumberMarker);
|
||||
dispatcherState.houseNumberMarker = null;
|
||||
}
|
||||
|
||||
function setHouseNumberMarker(latlng, label) {
|
||||
clearHouseNumberMarker();
|
||||
|
||||
const marker = L.marker(latlng, {
|
||||
title: label,
|
||||
keyboard: true,
|
||||
icon: L.divIcon({
|
||||
className: 'house-number-marker-icon',
|
||||
html: '<span class="house-number-flag"><span class="house-number-flag-label"></span></span>',
|
||||
iconSize: null,
|
||||
iconAnchor: [0, 34]
|
||||
})
|
||||
});
|
||||
|
||||
marker.addTo(map);
|
||||
|
||||
const labelElement = marker.getElement()?.querySelector('.house-number-flag-label');
|
||||
if (labelElement) labelElement.textContent = label;
|
||||
|
||||
marker.bindTooltip(tooltipElement(label), {direction: 'top', offset: [0, -8]});
|
||||
marker.on('click', event => {
|
||||
if (event.originalEvent) L.DomEvent.stopPropagation(event.originalEvent);
|
||||
clearHouseNumberMarker();
|
||||
setStreetSearchStatus('Markierung entfernt.');
|
||||
});
|
||||
|
||||
dispatcherState.houseNumberMarker = marker;
|
||||
}
|
||||
|
||||
function initStreetSearch() {
|
||||
const input = document.getElementById('streetSearchInput');
|
||||
if (!input) return;
|
||||
|
||||
input.addEventListener('input', handleStreetSearchInput);
|
||||
input.addEventListener('keydown', handleStreetSearchKeydown);
|
||||
input.addEventListener('focus', () => {
|
||||
if (input.value.trim()) updateStreetSearchSuggestions();
|
||||
});
|
||||
input.addEventListener('blur', () => {
|
||||
window.setTimeout(closeStreetSearchResults, 120);
|
||||
});
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Straßen-Dialog */
|
||||
/* ------------------------------------------------------------------ */
|
||||
@@ -1748,6 +2111,7 @@ function normalizeTimestamp(value) {
|
||||
|
||||
function bindStaticUi() {
|
||||
initTheme();
|
||||
initStreetSearch();
|
||||
|
||||
document.getElementById('baseLayerSelect').addEventListener('change', event => {
|
||||
setBaseLayer(event.target.value);
|
||||
|
||||
Reference in new issue
Block a user