Add Google Map API Key
This guide explains how to integrate the Google Maps API key into your Flutter project. You will configure the API key for Android, iOS, and Web platforms to enable Google Maps services like Directions, Distance Matrix, Geocoding, and more.
Step 1: Generate Google API Key
To generate your API key:
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Enable the following APIs:
- Direction API
- Distance Matrix API
- Geocoding API
- Maps SDK for Android
- Maps SDK for iOS
- Place API
- Follow the instructions to generate an API key by visiting the Google Maps API Key documentation.
- If prompted, set up a billing account (how to activate billing).
Step 2: Update Android Configuration
- Navigate to the
AndroidManifest.xml
file.- Location:
<project>/android/app/src/main/AndroidManifest.xml
- Location:
- Add the API key inside the
<application>
tag like so:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourcompany.yourproject">
<application>
<!-- Add Google Maps API Key -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_MAP_API_KEY_HERE" />
</application>
</manifest>
Step 3: Update iOS Configuration
- Open the
AppDelegate.swift
file.- Location:
<project>/ios/Runner/AppDelegate.swift
- Location:
- Add the following code to the
didFinishLaunchingWithOptions
method to provide the API key:
import UIKit
import Flutter
import GoogleMaps
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Provide Google Maps API key for iOS
GMSServices.provideAPIKey("YOUR_MAP_API_KEY_HERE")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Step 4: Run the Flutter Project
Once you've updated all configurations, you can run the project on the desired platform.
For Android & iOS:
-
Open the terminal in the project directory.
-
Run the following command to start the app:
flutter run
Troubleshooting
1. Verify API Key and Permissions
- Ensure that the correct API key is added in the configurations for all platforms (Android, iOS, and Web).
- Double-check that the necessary APIs (e.g., Directions API, Geocoding API) are enabled in the Google Cloud Console.
2. Verify Device/Emulator Connection
-
Check if your Android or iOS device is connected and recognized by Flutter by running the following command:
flutter devices