Adding location-based features can instantly elevate the utility of your mobile application. Whether you are building a delivery app or a store locator, knowing how to integrate google maps in flutter guide style is a fundamental skill for any modern Flutter developer. In my experience, the initial setup is where most developers stumble—usually due to a missing API key permission or a misplaced manifest entry.
In this tutorial, I will walk you through the exact process I use to get a fully functional map running on both iOS and Android. We will cover everything from the Google Cloud Console to handling the GoogleMap widget in your Dart code.
Prerequisites
Before we dive into the code, make sure you have the following ready:
- Flutter SDK installed and configured on your machine.
- A Google Cloud Project with a billing account linked (Google offers a generous free tier for Maps).
- An Android emulator or physical device and an iOS simulator.
- Basic familiarity with flutter responsive ui best practices to ensure your map scales correctly across screens.
Step 1: Get Your Google Maps API Key
You can’t render a map without an API key. Here is the process I follow:
- Go to the Google Cloud Console.
- Create a new project (e.g., “Flutter Maps Project”).
- Navigate to APIs & Services > Library.
- Enable the Maps SDK for Android and Maps SDK for iOS.
- Go to APIs & Services > Credentials and click “Create Credentials” to generate your API key.
Step 2: Configure Android Setup
For Android, you need to add your API key to the AndroidManifest.xml file. This is where most “blank map” errors come from.
Open android/app/src/main/AndroidManifest.xml and add the following metadata tag inside the <application> tag:
<manifest>
<application>
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR_ANDROID_API_KEY_HERE" />
</application>
</manifest>
Step 3: Configure iOS Setup
iOS requires a slightly different approach. You need to initialize the API key in your AppDelegate.
First, add the API key to your ios/Runner/AppDelegate.swift file:
import UIKit
import Flutter
import GoogleMaps // Import this
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) > Bool {
GMSServices.provideAPIKey("YOUR_IOS_API_KEY_HERE") // Add this line
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
As shown in the image below, ensure your AppDelegate is correctly importing the GoogleMaps module, otherwise, the project will fail to build.
Step 4: Implement the Google Map Widget
Now, let’s add the dependency. Run flutter pub add google_maps_flutter in your terminal. To keep your app lean, remember to check how to reduce flutter app size when adding heavy plugins like Maps.
Here is a complete implementation of a simple map screen:
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MapScreen extends StatefulWidget {
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
late GoogleMapController mapController;
// Initial position: San Francisco
final LatLng _center = const LatLng(37.7749, -122.4194);
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('My Flutter Map')),
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
markers: {
Marker(
markerId: const MarkerId('sf_marker'),
position: _center,
infoWindow: const InfoWindow(title: 'San Francisco', snippet: 'The Foggy City'),
),
},
),
);
}
}
Pro Tips for Advanced Integration
- Custom Marker Icons: Don’t stick to the red pin. Use
BitmapDescriptor.fromAssetImageto use your own branding. - Camera Updates: Use
mapController.animateCamerato smoothly transition the view when a user selects a location from a list. - Map Types: Switch between
MapType.normal,hybrid, andsatellitebased on user preference.
Troubleshooting Common Issues
| Issue | Common Cause | Solution |
|---|---|---|
| Blank screen / Grid lines | Invalid API Key or missing permissions | Verify API key in Cloud Console and check AndroidManifest.xml |
| iOS Crash on Startup | Missing GMSServices call | Check AppDelegate.swift for the provideAPIKey method |
| Slow Map Performance | Too many markers rendered at once | Implement marker clustering for large datasets |
What’s Next?
Now that you’ve mastered how to integrate google maps in flutter guide basics, you can take it further. I recommend exploring geolocator for real-time user tracking or flutter_polyline_points to draw routes between two locations. If you’re building a production app, don’t forget to restrict your API keys by package name/SHA-1 fingerprint in the Google Console to prevent unauthorized usage.