Google map is the best digital map in the world. So embed google map in android application is very user friendly and can improve user experience. Google provide android APIs for android developer to integrate google map service in their android application. This article will tell you how to do it step by step.
1. Apply Google Maps API Service Key Steps.
- First of all, you need to create a new google APIs project. Click google developer console to open it and login, then click CREATE PROJECT to create one project.
- Input the new project name and click Create button.
- When the project creation process complete successfully, you can see the new project in google APIs project list page. Click the project name and then click Google APIs menu —> APIs & Services —> Dashboard to open the project dashboard page.
- In the dashboard page, click google maps android api link.
- In the new page click Enable button, after a while you can go to below page, it prompt you to create a credential first.
- In the create credentials page, select Google Maps Android API in the drop down list then click What credentials do i need? button.
- Then google will give you a credential by your selected API service, because google map credential is a API key, so it give you an API key in the next credential detail page.
- After create the project credential, you had better configure it to restrict it’s usage for security reason. You can follow google prompts. We just restrict it to android apps.
- To restrict the google maps API key used only by android apps, follow below steps.
First get the android apps package name from your AndroidManifest.xml file.
Then get the debug SHA-1 certificate fingerprint from app / res / values / google_maps_api.xml file. The file will be generated automatically after you add google maps activity in you android app.
<resources> <!-- TODO: Before you run your application, you need a Google Maps API key. To get one, follow this link, follow the directions and press "Create" at the end: https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=B1:BE:24:B9:08:60:AF:89:9B:B5:FE:64:7F:68:FD:46:32:39:21:7F%3Bcom.dev2qa.googlemapproject You can also add your credentials to an existing key, using these values: Package name: B1:BE:24:B9:08:60:AF:89:9B:B5:FE:64:7F:68:FD:46:32:39:21:7F SHA-1 certificate fingerprint: B1:BE:24:B9:08:60:AF:89:9B:B5:FE:64:7F:68:FD:46:32:39:21:7F Alternatively, follow the directions here: https://developers.google.com/maps/documentation/android/start#get-key Once you have your key (it starts with "AIza"), replace the "google_maps_key" string in this file. --> <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyAUhMxGePOy5-iR8OfonR-gdPi1qNIfodM</string> </resources>
- Click Add package name and fingerprint button to add above value in it, click Save button to save the result.
2. Create Android Google Maps Example Projects.
- Create a new android studio project.
- Input new android project name and input project save location folder, click next.
- In next dialog select minimum SDK version click next. In the Add an Activity To Mobile dialog, choose Google Maps Activity, click next and input related data and click Finish button.
- If it show you below error messages in bottom Messages tab, then click Install missing platform(s) and sync project link to install the missed android sdk.
Error:Failed to find target with hash string ‘android-26’ in: C:\Users\Jerry\AppData\Local\Android\Sdk
- After that open app / res / values / google_maps_api.xml file in left android project panel. In the xml file detail content in right panel, replace the string placeholder YOUR_KEY_HERE with the google maps api service credential key you just generated in section 1.
- Find Google Maps API related play services description in this url : https://developers.google.com/android/guides/setup
- Open project build.gradle file, add above google maps API dependency library in it. You can find the dependency has been added by android studio automatically.
- Click and open app / res / layout / activity_google_maps.xml file. You can see that a Fragment has been added to this layout xml file automatically by wizard also.
- Click and open GoogleMapsActivity.java file, you can also find wizard has add code skeleton for you, so if you do not need to add more function, this app can be executed right now.