Integrating Google Maps into your Flutter app requires obtaining an API key from Google Cloud and configuring it within your project. This guide will walk you through the steps to obtain a Google Maps API key and configure it for both Android and iOS platforms.
Go to Google Cloud Console
Create a New Project
New Project.Create.Enable Google Maps APIs
APIs & Services -> Library.Google Maps SDK for Android and Google Maps SDK for iOS.Create API Credentials
APIs & Services -> Credentials.Create Credentials and select API Key.Restrict Your API Key (Recommended)
Edit icon next to your API key.Key restrictions, select HTTP referrers for web applications or IP addresses for server applications.API restrictions, select Restrict key and choose the APIs you enabled.Open Android Project
android directory.Add the API Key to AndroidManifest.xml
Open android/app/src/main/AndroidManifest.xml.
Add the following meta-data tag within the application tag:
<application>
<!-- Other configurations -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE"/>
</application>
Replace YOUR_API_KEY_HERE with your actual Google Maps API key.
Update build.gradle
android/app/build.gradle.dependencies {
implementation 'com.google.android.gms:play-services-maps:18.0.0'
}Open iOS Project
ios directory of your Flutter project and open Runner.xcworkspace in Xcode.Add the API Key to AppDelegate.swift
ios/Runner/AppDelegate.swift.GoogleMaps at the top:
import GoogleMapsapplication(_:didFinishLaunchingWithOptions:) method:
GMSServices.provideAPIKey("YOUR_API_KEY_HERE")YOUR_API_KEY_HERE with your actual Google Maps API key.Update Podfile
Open ios/Podfile.
Ensure that you have the following line in your Podfile:
pod 'GoogleMaps'
Run pod install in the ios directory to install the Google Maps SDK for iOS:
cd ios
pod install
Add Google Maps Widget
In your Flutter project, add a Google Maps widget to one of your screens to verify integration:
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MyMapScreen extends StatefulWidget {
@override
_MyMapScreenState createState() => _MyMapScreenState();
}
class _MyMapScreenState extends State<MyMapScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Google Maps'),
),
body: GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(37.7749, -122.4194),
zoom: 10,
),
),
);
}
}
Run the App
You have successfully obtained and configured a Google Maps API key for your Flutter project on both Android and iOS platforms. By following these steps, you have integrated Google Maps into your application, allowing you to utilize map features and services.
For more detailed information and advanced configurations, refer to the official Google Maps documentation and the Flutter Google Maps plugin.