A new Flutter project.
-
Add this to your package's pubspec.yaml file:
dependencies: map_view: "^0.0.12"
- Go to: https://console.developers.google.com/
- Enable
Google Maps Android API
- Enable
Google Maps SDK for iOS
- Under
Credentials
, chooseCreate Credential
.- Note: For development, you can create an unrestricted API key that can be used on both iOS & Android. For production it is highly recommended that you restrict.
- More detailed instructions for Android can be found here: https://developers.google.com/maps/documentation/android-api/signup
- More detailed instructions for iOS can be found here: https://developers.google.com/maps/documentation/ios-sdk/get-api-key
The way you register your API key on iOS vs Android is different. Make sure to read the next sections carefully.
The maps plugin will request your users location when needed. iOS requires that you explain this usage in the Info.plist file
- Set the NSLocationWhenInUseUsageDescription in
ios/Runner/Info.plist
. Example:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Using location to display on a map</string>
- Prior to using the Map plugin, you must call MapView.setApiKey(String apiKey). Example:
import 'package:map_view/map_view.dart';
void main() {
MapView.setApiKey("<your_api_key>");
runApp(new MyApp());
}
Note: If your iOS and Android API key are different, be sure to use your iOS API key here.
-
Add code to show the MapView.
//Create an instance variable for the mapView var _mapView = new MapView(); //Add a method to call to show the map. void showMap() { _mapView.show(new MapOptions(showUserLocation: true)); }
-
Run your application on an iOS device or simulator. Confirm that when you display the map you see map detail. If you only see a beige screen it's possible that your API key is incorrect. When your API key is incorrect you'll see messages like this in the console:
ClientParametersRequest failed, 7 attempts remaining (0 vs 12). Error Domain=com.google.HTTPStatus Code=400 "(null)" UserInfo={data=<>}
-
Your Bundle ID does not match what is registered in the Google API Console. When you create an restricted API key in the Google API console it asks you to specify your iOS bundle ID. Make sure that your iOS Bundle Identifier matches the one you registered in the console.
-
Using the wrong key. If you made a separate key for iOS and Android, make sure you are using the iOS key in the MapView.setApiKey() call.
You will be making multiple edits to your AndroidManifest.xml
file. In your Flutter project, you can
find this file location under android/app/src/main
-
In your
AndroidManifest.xml
, add the following uses-permission above the tag.<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
-
In your AndroidManifest.xml, add the following lines inside of the
application
tag. Be sure to replaceyour_api_key
with the one you generated.<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_api_key"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
-
Add the MapActivity to your AndroidManifest.xml
<activity android:name="com.apptreesoftware.mapview.MapActivity" android:theme="@style/Theme.AppCompat.Light.DarkActionBar"/>
-
In your
android/build.gradle
file. UnderbuildScript
dependencies
add:classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-4'
-
Run your application on an Android device or simulator. Confirm that when you display the map you see map detail. If you only see a beige screen it's possible that your API key is incorrect.
For help getting started with Flutter, view our online documentation.