flutter google maps app
a new flutter project.
getting started
flutter project
- add this to your package’s pubspec.yaml file:
dependencies: map_view: "^0.0.12"
getting started
generate your api key
- 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.
- note: for development, you can create an unrestricted api key that can be used on both ios & android.
- 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.
ios
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=<>}
common api key problems for ios
- 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.
android
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.
Comments are closed.