Add real-time autocomplete suggestions when typing in the place search

field, with Google Places Autocomplete API and Nominatim fallback.

  - Add POST /api/maps/autocomplete route and autocompletePlaces service
  - Add mapsApi.autocomplete client method
  - Add debounced autocomplete dropdown to PlaceFormModal with keyboard
    navigation (arrow keys, enter, escape) and mouse selection
  - Use place details API to populate form fields on suggestion selection
  - Derive location bias from existing trip places for better results
  - Extract Google Maps URL regex to shared constant
This commit is contained in:
Ben Haas
2026-04-09 12:20:03 -07:00
parent 0df90086bf
commit 35d676e76e
4 changed files with 261 additions and 18 deletions
+30
View File
@@ -7,6 +7,7 @@ import {
getPlacePhoto,
reverseGeocode,
resolveGoogleMapsUrl,
autocompletePlaces,
} from '../services/mapsService';
const router = express.Router();
@@ -29,6 +30,35 @@ router.post('/search', authenticate, async (req: Request, res: Response) => {
}
});
// POST /autocomplete
router.post('/autocomplete', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
const { input, lang, locationBias } = req.body;
if (!input || typeof input !== 'string') {
return res.status(400).json({ error: 'Input is required' });
}
if (locationBias && (!Number.isFinite(locationBias.lat) || !Number.isFinite(locationBias.lng))) {
return res.status(400).json({ error: 'Invalid locationBias: lat and lng must be finite numbers' });
}
try {
const result = await autocompletePlaces(
authReq.user.id,
input,
lang as string,
locationBias as { lat: number; lng: number } | undefined,
);
res.json(result);
} catch (err: unknown) {
const status = (err as { status?: number }).status || 500;
const message = err instanceof Error ? err.message : 'Autocomplete error';
console.error('Maps autocomplete error:', err);
res.status(status).json({ error: message });
}
});
// GET /details/:placeId
router.get('/details/:placeId', authenticate, async (req: Request, res: Response) => {
const authReq = req as AuthRequest;
+87
View File
@@ -32,6 +32,16 @@ interface GooglePlaceResult {
types?: string[];
}
interface GoogleAutocompleteSuggestion {
placePrediction?: {
placeId: string;
structuredFormat?: {
mainText?: { text: string };
secondaryText?: { text: string };
};
};
}
interface GooglePlaceDetails extends GooglePlaceResult {
userRatingCount?: number;
regularOpeningHours?: { weekdayDescriptions?: string[]; openNow?: boolean };
@@ -303,6 +313,83 @@ export async function searchPlaces(userId: number, query: string, lang?: string)
return { places, source: 'google' };
}
// ── Autocomplete (Google or Nominatim fallback) ─────────────────────────────
export async function autocompletePlaces(
userId: number,
input: string,
lang?: string,
locationBias?: { lat: number; lng: number },
): Promise<{ suggestions: { placeId: string; mainText: string; secondaryText: string }[]; source: string }> {
const apiKey = getMapsKey(userId);
if (!apiKey) {
return autocompleteNominatim(input, lang);
}
const body: Record<string, unknown> = {
input,
languageCode: lang || 'en',
};
if (locationBias) {
body.locationBias = {
circle: {
center: { latitude: locationBias.lat, longitude: locationBias.lng },
radius: 50000.0,
},
};
}
const response = await fetch('https://places.googleapis.com/v1/places:autocomplete', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Goog-Api-Key': apiKey,
},
body: JSON.stringify(body),
});
const data = await response.json() as { suggestions?: GoogleAutocompleteSuggestion[]; error?: { message?: string } };
if (!response.ok) {
const err = new Error(data.error?.message || 'Google Places Autocomplete error') as Error & { status: number };
err.status = response.status;
throw err;
}
const suggestions = (data.suggestions || [])
.filter((s) => s.placePrediction)
.slice(0, 5)
.map((s) => ({
placeId: s.placePrediction!.placeId,
mainText: s.placePrediction!.structuredFormat?.mainText?.text || '',
secondaryText: s.placePrediction!.structuredFormat?.secondaryText?.text || '',
}));
return { suggestions, source: 'google' };
}
async function autocompleteNominatim(
input: string,
lang?: string,
): Promise<{ suggestions: { placeId: string; mainText: string; secondaryText: string }[]; source: string }> {
try {
const places = await searchNominatim(input, lang);
const suggestions = places.slice(0, 5).map((p) => {
const parts = (p.address || '').split(',').map((s) => s.trim());
return {
placeId: p.osm_id || '',
mainText: p.name || parts[0] || '',
secondaryText: parts.slice(1).join(', '),
};
});
return { suggestions, source: 'nominatim' };
} catch (err) {
console.error('Nominatim autocomplete failed:', err);
return { suggestions: [], source: 'nominatim' };
}
}
// ── Place details (Google or OSM) ────────────────────────────────────────────
export async function getPlaceDetails(userId: number, placeId: string, lang?: string): Promise<{ place: Record<string, unknown> }> {