Overview
TheInputLocation component provides location autocomplete functionality powered by the Google Maps Places API. It requires both the API script and the @react-google-maps/api package.
Prerequisites
1. Install Required Package
2. Get Google Maps API Key
Create Google Cloud Project
Navigate to the Google Cloud Console and create a new project or select an existing one.
Enable Places API
Go to APIs & Services > Library and enable:
- Places API
- Maps JavaScript API (required for Places API to work)
Setup in Your Application
Option 1: Using Script Tag (Recommended)
Add the Google Maps API script to your application’s HTML<head> section:
index.html
key: Your Google Maps API keylibraries=places: Loads the Places librarylanguage=es: Sets default language to Spanish (component is Spanish-localized)
Option 2: Using useJsApiLoader Hook
For dynamic loading with React:App.tsx
Environment Variables
Store your API key securely using environment variables:Usage Example
LocationForm.tsx
Component Props
Required Props
Must be
true when Google Maps API is fully loaded. The component is disabled until this is true.Optional Props
Google Places API search types. Options include
"address", "geocode", "establishment", "(regions)", "(cities)".Place details fields to fetch. Options include
"geometry", "address_components", "formatted_address", "name", etc.When
true, only shows street name and number in the input after selection (useful for form inputs). When false, shows the full address.Callback fired when user selects a location from autocomplete suggestions.
Location Data Structure
TheonLocationChange callback receives a LocationProps object:
Component Behavior
- Debounced Search: Autocomplete requests are debounced by 500ms to reduce API calls
- Spanish Localization: Search and error messages are in Spanish
- Validation: Requires complete address with street number and postal code
- Error Handling: Shows validation errors in Spanish below the input
- Loading State: Displays “Cargando…” placeholder while API loads
Troubleshooting
Component stays disabled with “Cargando…” placeholder
Solution
Solution
- Verify
isLoadedprop istrue - Check browser console for Google Maps API errors
- Ensure API key is valid and not restricted
- Verify Places API is enabled in Google Cloud Console
No autocomplete suggestions appear
Solution
Solution
- Check API key billing is enabled (Google requires billing for Places API)
- Verify API key restrictions allow your domain
- Check network tab for failed API requests
- Ensure
libraries=placesis included in script URL
Error: “Necesitamos una dirección más precisa…”
Solution
Solution
This is expected behavior when the selected location lacks a street number or postal code. The user must select a more specific address.
API Costs
Google Maps Platform charges per API request. Places Autocomplete costs $2.83-17.00 per 1000 requests depending on usage. Monitor your usage in the Google Cloud Console.Cost-saving tips:
- Use session tokens (automatically handled by
@react-google-maps/api) - The component’s 500ms debounce reduces unnecessary requests
- Set up billing alerts in Google Cloud Console
Next Steps
InputLocation Component
View full component documentation and examples
Optional Dependencies
Learn about other optional peer dependencies