Requirements
Google Maps
Create and restrict a Google Maps JavaScript API key for maps and geolocation.
This document describes how to set up a Google Maps JavaScript API key for the application.
Overview
The application uses the Google Maps JavaScript SDK to display interactive maps and the Geolocation API for location-based features (postal code geolocation). This guide walks through creating a Google Cloud project, enabling the necessary APIs, and securing your API key.
Environment Configuration
Once you have followed the steps below, you will add the following credentials
to your .env file:
GEOCODING_API_KEY=AIzaSyBxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Prerequisites
- A Google account
- Access to the Google Cloud Console
Step 1: Create a Google Cloud Project
- Navigate to the Google Cloud Console
- Click the project dropdown at the top of the page (next to "Google Cloud")
- Click New Project in the modal that appears
- Enter a project name (e.g., "My Website Maps")
- (Optional) Select a billing account if you have one
- Click Create
- Wait for the project to be created, then select it from the project dropdown
Step 2: Enable the Maps JavaScript API
- In the Google Cloud Console, ensure your project is selected
- Navigate to APIs & Services > Library from the navigation menu
- Search for "Maps JavaScript API" in the search bar
- Click on Maps JavaScript API from the results
- Click the Enable button
- Wait for the API to be enabled (this may take a few moments)
Step 3: Enable the Geolocation API
- While still in the API Library, search for "Geolocation API"
- Click on Geolocation API from the results
- Click the Enable button
- Wait for the API to be enabled
Note: You may also want to enable additional APIs depending on your needs:
- Places API - For place searches and autocomplete
- Geocoding API - For converting addresses to coordinates
- Directions API - For route calculations
Step 4: Create an API Key
- Navigate to APIs & Services > Credentials from the navigation menu
- Click Create Credentials at the top of the page
- Select API Key from the dropdown
- A modal will appear showing your new API key
- Important: Copy this key immediately and store it securely
- Click Close (or Restrict Key to proceed directly to restrictions)
Your API key will look something like this:
AIzaSyBxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Step 5: Restrict and Secure Your API Key
Important: Always restrict your API keys to prevent unauthorized use and unexpected charges.
Set Application Restrictions
- In the Credentials page, click on your API key name
- Under Application restrictions, select HTTP referrers (web sites)
- Click Add an item under Website restrictions
- Add your authorized domains in the following format:
https://yourdomain.com/* https://www.yourdomain.com/* - For local development, add:
http://localhost:* http://127.0.0.1:* - Click Done
Set API Restrictions
- Scroll down to API restrictions
- Select Restrict key
- Check the boxes for the APIs you enabled:
- Maps JavaScript API
- Geolocation API
- (Any other APIs you're using)
- Click Save at the bottom of the page
Best Practices for API Key Security
- Never commit API keys to version control - Use environment variables
- Use separate keys for development and production - Create different keys with different restrictions
- Monitor API usage - Regularly check the Google Cloud Console for unexpected usage
- Set up billing alerts - Configure alerts to notify you if costs exceed expected thresholds
- Regenerate keys if compromised - If a key is exposed, create a new one and delete the old one
Setting Up Billing (Required)
Google Maps Platform requires a billing account to be linked to your project, even though there is a monthly free tier of $200 in usage credits.
- Navigate to Billing in the Google Cloud Console
- Click Link a billing account or Create billing account
- Follow the prompts to add payment information
- Review the pricing information to understand costs
Free Tier: Google provides $200 in free usage credits per month, which covers most small to medium-sized applications.
Troubleshooting
"This page can't load Google Maps correctly"
- Verify your API key is correct
- Ensure the Maps JavaScript API is enabled
- Check that your domain is listed in the HTTP referrer restrictions
- Verify billing is set up for your project
"RefererNotAllowedMapError"
- Your domain is not authorized in the API key restrictions
- Add your domain to the HTTP referrers list (don't forget the
/*suffix)
Geolocation Not Working
- Ensure the Geolocation API is enabled
- Verify your API key has access to the Geolocation API in the API restrictions