Feature: Location Search (Google Places)
Relevant source files
The following files were used as context for generating this wiki page:The Location Search feature provides users with the ability to search for and select real-world locations, powered by the Google Places API. This functionality is integrated into the application to allow location data to be associated with other features, such as calendar events. The system is composed of a frontend React component for user interaction, a backend tRPC API for handling requests, and a dedicated provider that communicates directly with the Google Places service.
This feature relies on external Google Cloud services and requires specific API keys and enabled services to function correctly. The implementation includes debouncing on the client-side and rate-limiting on the server-side to ensure efficient and controlled usage of the Google Places API.
1. System Configuration
Proper configuration is required to enable the Google Places integration. This involves setting up environment variables and ensuring the correct dependencies are installed.
1.1. API Key
A Google Maps API key is mandatory. The application expects this key to be available in an environment variable named GOOGLE_MAPS_API_KEY.
To set this up:
- Enable the Places API (New) in your Google Cloud project.
- Create an API key from the Google Cloud Maps Platform Credentials page.
- Set the created key as
GOOGLE_MAPS_API_KEYin the project's.envfile.
Sources: README.md:61-68
1.2. Dependencies
The backend API service relies on the official Google Maps client library for Node.js.
| Package Name | Version | Purpose |
|---|---|---|
@googlemaps/places | ^2.1.0 | Client for the Google Places API. |
Sources: packages/api/package.json:28
2. Backend Architecture
The backend is built using tRPC and consists of a router that exposes endpoints to the client and a provider class that encapsulates the logic for interacting with the Google Places API.
This diagram shows the flow of a request from the tRPC router to the Google Places API via the GooglePlacesProvider.
2.1. Google Places Provider
The GooglePlacesProvider class is the sole interface to the Google Places API within the backend. It initializes the @googlemaps/places client using the GOOGLE_MAPS_API_KEY from the environment.
Sources: packages/api/src/providers/google-places/google-places.ts:5-11
Key Methods
-
search(query: string): This method takes a search string and uses thePlacesApiClient.searchTextmethod to find matching places. It specifically requests theid,displayName, andformattedAddressfor each place. The results are then mapped to a simplified format before being returned.
Sources: packages/api/src/providers/google-places/google-places.ts:13-26 -
getDetails(placeId: string): This method retrieves detailed information for a specific place using itsplaceId. It callsPlacesApiClient.getPlaceand requests theid,displayName,formattedAddress, andlocation(latitude/longitude).
Sources: packages/api/src/providers/google-places/google-places.ts:28-40
2.2. API Endpoints (tRPC)
The placesRouter defines two protected procedures that are exposed to the frontend. Both endpoints are rate-limited to prevent abuse.
Sources: packages/api/src/routers/places.ts:8-11
places.search
This endpoint is used for autocompleting location searches.
| Parameter | Type | Description |
|---|---|---|
query | string (min: 1) | The search term entered by the user. |
The endpoint returns an array of objects, each containing:
id: The Google Place ID.name: The display name of the place.address: The formatted address of the place.
Sources: packages/api/src/routers/places.ts:12-20
places.getDetails
This endpoint fetches detailed information for a selected place.
| Parameter | Type | Description |
|---|---|---|
placeId | string (min: 1) | The unique ID of the place from Google. |
The endpoint returns a single object with:
id: The Google Place ID.name: The display name of the place.address: The formatted address.latitude: The latitude coordinate.longitude: The longitude coordinate.
Sources: packages/api/src/routers/places.ts:22-29
3. Frontend Implementation
The AddressCombobox is a React component that provides the user interface for searching locations. It features a text input that, when typed into, queries the backend for location suggestions.
3.1. Component Logic
- State Management: The component manages the user's input
queryin its local state. - Debouncing: User input is debounced by 300ms using a
useDebouncehook. This ensures that the API is not called on every keystroke, improving performance and reducing costs. - Data Fetching: The tRPC hook
trpc.places.search.useQueryis used to fetch search results based on thedebouncedQuery. The query is only enabled when the debounced query string is not empty. - Rendering: Search results are displayed in a dropdown list. A loading indicator is shown while data is being fetched.
- Value Selection: When a user selects a location from the list, the
onValueChangecallback prop is invoked with the selected place's data (id,name,address). The component then resets its internal state.
Sources: apps/web/src/components/address-combobox.tsx:19-48
3.2. User Interaction Flow
The following sequence diagram illustrates the end-to-end data flow, from the user typing in the combobox to the results being displayed.
4. Summary
The Location Search feature is a well-defined, three-tier system that cleanly separates concerns between the user interface, the application's backend API, and the external Google Places service. The frontend AddressCombobox component provides a responsive user experience with debouncing, while the backend tRPC router secures the endpoints with authentication and rate-limiting. The GooglePlacesProvider acts as a clean abstraction over the external API, making the integration maintainable and easy to understand.