Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions src/geocoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function zoomForAddrType(addrType: string): number {
// Shared references so addSearchControl can clear the drop pin / geocode bar on marker click.
let _pinLayer: L.LayerGroup | null = null;
let _clearSearchSelection: (() => void) | null = null;
let _showGeocodeBar: ((label: string, copyText: string) => void) | null = null;
let _showGeocodeBar: ((label: string, copyText: string, latlng: L.LatLng) => void) | null = null;

export function addSearchControl(map: L.Map, state: AppState, onNoResults: (message: string) => void): void {
// state is read in the results callback (for future extensibility)
Expand Down Expand Up @@ -266,7 +266,7 @@ export function addSearchControl(map: L.Map, state: AppState, onNoResults: (mess
const coordText = `${lat.toFixed(5)}, ${lng.toFixed(5)}`;
const label = resultName !== '' ? resultName : coordText;
const copyText = resultName !== '' ? `${resultName}\n${coordText}` : coordText;
_showGeocodeBar?.(label, copyText);
_showGeocodeBar?.(label, copyText, L.latLng(lat, lng));
clearSelection();
li.classList.add('sheet-result--active');
li.classList.remove('sheet-result--expanded');
Expand Down Expand Up @@ -471,13 +471,16 @@ export function addReverseGeocoding(
const barNavBtn = geocodeBar.querySelector<HTMLButtonElement>('.geocode-bar__nav')!;
const barHandle = geocodeBar.querySelector<HTMLElement>('.geocode-bar__handle')!;

// "Navigate" button on the geocode-bar — start guidance to the dropped pin's
// current latlng. The pin location is on whichever marker is in pinLayer
// (there's always at most one when the bar is visible).
// "Navigate" button on the geocode-bar — start guidance to the destination
// associated with the currently-shown bar. showGeocodeBar() is the single
// setter so search-results, long-press, and pin-drag all stay in sync.
let currentPinLatLng: L.LatLng | null = null;
let currentPinLabel = '';
barNavBtn.addEventListener('click', () => {
if (currentPinLatLng === null) return;
if (currentPinLatLng === null) {
showToast('No destination set');
return;
}
void startGuidance(
state,
map,
Expand All @@ -486,16 +489,6 @@ export function addReverseGeocoding(
);
});

// Expose pin position to the Navigate button via closure-captured ref.
// Updated by showGeocodeBar through pinLayer inspection on each open.
pinLayer.on('layeradd', () => {
const layers = pinLayer.getLayers();
const pin = layers[layers.length - 1];
if (pin instanceof L.Marker) {
currentPinLatLng = pin.getLatLng();
}
});

// Tap-to-expand: tapping a truncated address shows the full text for 3s
// (or until a second tap). This makes long addresses readable on mobile
// where title-attr tooltips don't render.
Expand Down Expand Up @@ -589,14 +582,15 @@ export function addReverseGeocoding(
}
}

function showGeocodeBar(label: string, copyText: string): void {
function showGeocodeBar(label: string, copyText: string, latlng: L.LatLng): void {
collapseAddr();
barAddrEl.textContent = label;
barAddrEl.title = label;
barCopyBtn.dataset['copy'] = copyText;
barCopyBtn.textContent = 'Copy';
barCopyBtn.classList.remove('geocode-bar__copy--copied');
currentPinLabel = label;
currentPinLatLng = latlng;

if (sheetState === 'hidden') {
// Render off-screen first so offsetHeight is available, then use double-rAF
Expand Down Expand Up @@ -780,12 +774,12 @@ export function addReverseGeocoding(
if (error || !result) {
// Geocoding failed — fall back to coordinates.
document.title = coordLabel;
showGeocodeBar(coordLabel, coordLabel);
showGeocodeBar(coordLabel, coordLabel, latlng);
return;
}
const addr = result.address.Match_addr;
document.title = addr;
showGeocodeBar(addr, addr);
showGeocodeBar(addr, addr, latlng);
});
}

Expand Down
Loading