Google Map Example In Android Studio

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.

  1. 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.
    create google api project
  2. Input the new project name and click Create button.
    input new google api project name
  3. 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.
    select project and open google apis & services dashboard
  4. In the dashboard page, click google maps android api link.
    select google maps android api
  5. In the new page click Enable button, after a while you can go to below page, it prompt you to create a credential first.
    enable google maps api create credentials page
  6. In the create credentials page, select Google Maps Android API in the drop down list then click What credentials do i need? button.
    google maps api credentials creation page
  7. 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.
    create google credential api key detail info page
  8. 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.
    restrict google apis service credential usage settings page
  9. 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>
  10. Click Add package name and fingerprint button to add above value in it, click Save button to save the result.
    add package name and sha-1 key fingerprint to android google map api
READ :   Simple Android News App Compatible With Phones and Tablets

2. Create Android Google Maps Example Projects.

  1. Create a new android studio project.
    android studio create new project
  2. Input new android project name and input project save location folder, click next.
    input android project name and select project location
  3. 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.
    choose google maps activity in add activity to mobile popup dialog
  4. 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
  5. 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.
    input google maps api key in google_maps_api.xml file
  6. Find Google Maps API related play services description in this url : https://developers.google.com/android/guides/setup
    google play services description for google maps api
  7. 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.
    com.google.android.gms:play-services-maps:12.0.0
    add google maps api dependency in build.gradle file
  8. 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.
  9. 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.
(Visited 1,301 times, 10 visits today)

Leave a Reply

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.