Google Map Example In Android Studio

Google map is the best digital map in the world. So embed google Maps in the android applications is very user-friendly and can improve user experience. Google provides android APIs for android developers to integrate the 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.

  1. First of all, you need to create a new google APIs project. Click the google developer console to open it and log in, then click the CREATE PROJECT button to create a new project.
  2. Input the new project name in the Project name input text box and click the Create button to create it.
  3. When the project creation process completes successfully, you can see the new project on the google APIs project list page.  Click the project name and then click Google APIs menu —> APIs & Services —> Dashboard to open the project dashboard page.
  4. On the dashboard page, click the Google Maps Android API link.
  5. On the new page click the Enable button, after a while, you will go to the Google Maps Android API page, it will show the warning message to let you to create a credential first. The warning message is To use this API, you may need credentials. Click ‘ Create credentials ‘ to get started.
  6. When you click the Create credentials button int the Google Maps Android API page, it will direct you to the create credentials page, select Google Maps Android API in the Add credentials to your project section drop-down list then click the What credentials do I need? button.
  7. Then google will give you a credential by your selected API service because Google Maps credential is an API key, so it gives you an API key in the Get your credentials page.
  8. After creating the project credential, you had better configure it to restrict it’s usage for security reasons. You can follow google prompts. We just restrict it to android apps.
  9. To restrict the google maps API key used only by android apps, follow the below steps.
  10. First, get the android apps package name from your AndroidManifest.xml file.
  11. Then get the debug SHA-1 certificate fingerprint from the app/res/values/google_maps_api.xml file. The file will be generated automatically after you add google maps activity in your 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>
  12. Now go back to the Google Maps APIs & Services page, click the credentials link on the page left side, then click the Application restrictions on the page right side API Key section.
  13. Select the Android apps radio button, and click the Add package name and fingerprint button on the bottom of the page to add the above Package name and SHA-1 certificate fingerprint value in it, click the Save button to save the result.

2. Create Android Google Maps Example Projects.

  1. Create a new android studio project.
  2. Input new android project name and input project save location folder, click the Next button.
  3. In the next dialog, select the minimum SDK version then click the Next button.
  4. In the Add an Activity To Mobile dialog, choose Google Maps Activity, click the Next button and input related data and click the Finish button.
  5. If it shows you the below error messages in the 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
  6. After that open the app/res/values/google_maps_api.xml file in the left android project panel. In the XML file detail content in the right panel,  replace the string placeholder YOUR_KEY_HERE with the google maps API service credential key you just generated in section 1.
  7. Find Google Maps API related play services description information in this url: https://developers.google.com/android/guides/setup
  8. 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.
    dependencies {
         implementation 'com.google.android.gms:play-services-maps:12.0.0'
    }
  9. Click and open the 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.
  10. Click and open the GoogleMapsActivity.java file, you can also find that the wizard has added a code skeleton for you, so if you do not need to add more functions, this app can be executed right now.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.