Setup
Official docs: https://developers.google.com/maps/flutter-package/config
Step 1: Set the platform version
- Android
- IOS
Set the minimum SDK version for Android.
android {
defaultConfig {
applicationId "com.example.google_maps_in_flutter"
minSdkVersion 21 // Set to 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
}
Set the minimum iOS platform version.
- Open the
ios/Podfile
config file in your preferred IDE. - Add the following lines to the beginning of this Podfile:
# Set platform to 14.0 to enable latest Google Maps SDK
platform :ios, '14.0'
Step 2: Add your API key to the project
To avoid disclosing the API_KEY
when publicly releasing the source code, we will not push the key to a remote server.
- Android
- IOS
-
Create file
appconfig.properties
in folderandroid/
, with contentandroid/appconfig.propertiesMAP_API_KEY=[Put your Map API key here]
-
Add line to android/.gitignore
appconfig.properties
-
Run the command below to clear cache git
git rm -r --cached .
-
Open file
android/app/build.gradle
add the following highlighted lines- Groovy
- Kotlin
build.gradle
...
def appConfigProperties = new Properties()
def appConfigPropertiesFile = rootProject.file('appconfig.properties')
if (appConfigPropertiesFile.exists()) {
appConfigPropertiesFile.withReader('UTF-8') { reader ->
appConfigProperties.load(reader)
}
}
def mapApiKey =appConfigProperties.getProperty('MAP_API_KEY')
android {
...
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.example.demo_flutter_google_map_marker"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
manifestPlaceholders += [MAP_API_KEY: mapApiKey]
}
...
}build.gradle.ktsimport java.util.Properties
...
val appConfigPropertiesFile = rootProject.file("appconfig.properties")
val appConfigProperties = Properties()
if (appConfigPropertiesFile.exists()) {
appConfigPropertiesFile.inputStream().use { input ->
appConfigProperties.load(input)
}
}
val mapApiKey = appConfigProperties.getProperty("MAP_API_KEY")
android {
...
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.example.demo_flutter_google_map_marker"
// You can update the following values to match your application needs.
// For more information, see: https://flutter.dev/to/review-gradle-config.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
manifestPlaceholders["MAP_API_KEY"] = mapApiKey
}
...
} -
In your
AndroidManifest.xml
file, go tocom.google.android.geo.API_KEY
and update theandroid:value
attribute. If the<meta-data>
tag does not exist, create it as a child of the<application>
tag.<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAP_API_KEY}" />
-
Create file
AppConfig.xcconfig
in folderios/Flutter/
, and add this contentios/Flutter/AppConfig.xcconfigMAP_API_KEY=[Put your Map API key here]
Open
ios/Flutter/Debug.xcconfig
file and add line#include "AppConfig.xcconfig"
Open
ios/Flutter/Release.xcconfig
file and add line#include "AppConfig.xcconfig"
-
Add line to ios/.gitignore
AppConfig.xcconfig
-
Run the command below to clear cache git
git rm -r --cached .
-
Open the
ios/Runner/Info.plist
file and add following lines<key>MapApiKey</key>
<string>$(MAP_API_KEY)</string> -
Open the ios/Runner/AppDelegate.swift file in your Flutter project and add following highlighted lines
AppDelegate.swiftimport Flutter
import UIKit
import GoogleMaps
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let mapApiKey: String = Bundle.main.object(forInfoDictionaryKey: "MapApiKey") as! String
GMSServices.provideAPIKey(mapApiKey)
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}