From ef3533078c606665414c9fd5b7b857d3cc720016 Mon Sep 17 00:00:00 2001 From: Test Date: Sat, 25 Apr 2026 22:48:59 -0700 Subject: [PATCH] fix(geocoding): thread latlng through showGeocodeBar so Navigate works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "Navigate" button on the geocode-bar tracked its destination via a pinLayer 'layeradd' listener. That fires only when a new layer is added, which misses two flows: - Search "Go to location" calls pinLayer.clearLayers() and never adds a new pin, so currentPinLatLng stays null (silent fail) or stale (worse — navigates to the previous long-press pin). - Pin drag moves the same marker without re-adding, so the listener doesn't fire and Navigate goes to the original drop location, not the new dragged one. showGeocodeBar() is the single setter all three flows (search, long-press, drag) already converge on, so threading the L.LatLng through it captures the current destination reliably. Removed the 'layeradd' listener; replaced the silent-return guard with a toast so any future regression surfaces visibly. Closes #173 Co-Authored-By: Claude Opus 4.7 (1M context) --- src/geocoding.ts | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/geocoding.ts b/src/geocoding.ts index d737f1a..9006859 100644 --- a/src/geocoding.ts +++ b/src/geocoding.ts @@ -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) @@ -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'); @@ -471,13 +471,16 @@ export function addReverseGeocoding( const barNavBtn = geocodeBar.querySelector('.geocode-bar__nav')!; const barHandle = geocodeBar.querySelector('.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, @@ -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. @@ -589,7 +582,7 @@ 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; @@ -597,6 +590,7 @@ export function addReverseGeocoding( 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 @@ -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); }); }