Overview
React Route Finder uses the Google Maps JavaScript API to display interactive maps and calculate route directions. This guide covers obtaining an API key, configuring the integration, and security best practices.Required Google Maps APIs
The application requires two Google Maps APIs:- Maps JavaScript API: Displays the interactive map and route visualization
- Directions API: Calculates optimal routes between bus stops
Both APIs must be enabled in your Google Cloud Console project for the application to work correctly.
Obtaining a Google Maps API Key
Create a Google Cloud Project
- Go to Google Cloud Console
- Click “Select a project” → “New Project”
- Enter a project name (e.g., “React Route Finder”)
- Click “Create”
Enable Required APIs
- Navigate to “APIs & Services” → “Library”
- Search for “Maps JavaScript API” and click “Enable”
- Search for “Directions API” and click “Enable”
- Wait for both APIs to be activated
Create API Credentials
- Go to “APIs & Services” → “Credentials”
- Click “Create Credentials” → “API key”
- Copy the generated API key immediately
- Click “Edit API key” to configure restrictions
Restrict Your API Key
Configure restrictions to protect your API key:Application Restrictions:
- Select “HTTP referrers (web sites)”
- Add your domain:
yourdomain.com/* - Add localhost for development:
localhost/*
- Select “Restrict key”
- Choose “Maps JavaScript API”
- Choose “Directions API”
Configuration
Adding the API Key
The Google Maps API is loaded inpublic/index.html:
Current Configuration
The currentindex.html includes:
Configuration Options
You can add additional parameters to the Google Maps script URL:URL Parameters
- key (required): Your Google Maps API key
- libraries: Additional API libraries (places, geometry, drawing, etc.)
- language: Interface language (en, es, fr, etc.)
- region: Regional bias for geocoding
- v: API version (defaults to weekly)
Environment-Based Configuration
Using Build-Time Variables
For better security, inject the API key during the build process:Map Container Configuration
The map is rendered in a container div defined inindex.html:
Styling the Map Container
The current CSS defines dimensions:Responsive Map Sizing
For responsive layouts:Security Best Practices
API Key Protection
Use Application Restrictions
Always restrict your API key to specific domains:
- Production:
yourdomain.com/* - Staging:
staging.yourdomain.com/* - Development:
localhost/*
Set Usage Quotas
Configure daily quotas to prevent excessive charges:
- Go to “APIs & Services” → “Quotas”
- Set limits for each API
- Enable billing alerts
Never Commit API Keys
Protect your API keys from version control:Usage Limits and Billing
Free Tier
Google Cloud provides $200 monthly credit that covers:- Maps JavaScript API: 28,000 map loads per month
- Directions API: 40,000 requests per month
Cost Optimization
To minimize costs:- Cache Directions: Store frequently requested routes
- Debounce Requests: Limit API calls during user interaction
- Client-side Routing: Calculate simple routes client-side when possible
- Static Maps: Use static maps for non-interactive views
Example: Request Caching
Troubleshooting
API Key Errors
Error: “This page can’t load Google Maps correctly”- Verify the API key is correct in
index.html - Check that Maps JavaScript API is enabled
- Verify billing is enabled in Google Cloud Console
- Add your domain to HTTP referrer restrictions
- Include both
yourdomain.comandwww.yourdomain.com - Add
localhostfor local development
Map Not Displaying
Black or gray box instead of map:- Ensure the map container has explicit dimensions (height and width)
- Verify the Google Maps script loads before your application bundle
- Check browser console for JavaScript errors
- Check that Directions API is enabled
- Verify valid coordinates are being used
- Inspect network tab for failed API requests
Performance Issues
Slow map loading:- Use the
v=weeklyparameter for consistent versions - Load the Google Maps script with
async deferattributes - Consider lazy-loading the map for below-the-fold content
- Implement request caching
- Debounce user input that triggers API calls
- Use client-side geometry calculations when possible