Android
Google Maps SDK for Android Part 1

Google Maps SDK for Android Part 1

Have you tried to integrate Google Maps in your Android application? While Google provides Maps SDK for Android to integrate Google Maps in an Android Application with an extensive documentation, sometimes it becomes very difficult to follow the documentation, and we get confused with all the features this SDK provides. So to introduce you to most of the features provided by Google Maps Android I have planned a tutorial series comprising of multiple posts. Each of these posts will be an extension of the previous post introducing the implementation of new feature or functionality of Google Maps SDK for Android. So this is the first post in this tutorial series introducing “How to add Google Maps to an Android App?”. After completing all the steps provided in this post, you would have implemented a basic Google Map (with minimum features) in your Android App.




Pre-requisites:

Before following the steps given below there are some pre-requisites that you need to complete.

  • You should be developing your Android Project on Android Studio.
  • If you want support for Kotlin i.e you want to develop your Android App in Kotlin then you must have Android Studio with version equal to greater than 3.0.0. You can download the latest Android Studio from here.
  • Google Maps SDK is available through Google Play Services. If Google Play Services SDK is not already added to your Android Studio, then go to
    SDK Manager -> SDK Tools in Android Studio and download it. See below image for reference.

Android Studio SDK Manager


 

Now follow the steps given below.

STEP 1Create a new Project

If you already have an Android Project created in Android Studio then follow from step two, otherwise follow the below points.

  • To create a new Project go to File -> New -> New Project.
  • Enter your Application name and Company domain in the window that appears. Also check the box for Include Kotlin support, if you want to develop your Application in Kotlin language. Then click Next.
Create a New Project in Android Studio

Create a New Project in Android Studio

  • Select the Target Android Devices in the next window and click Next.
Android Target SDK

Android Target SDK

  • Now in the next window select Google Maps Activity and click Next.
Add Google Maps Activity

Add Google Maps Activity

  • In the last window, give a name for your Activity and Layout file. And click Finish. This will generate a Maps Activity for you with a layout file.
  • Now follow from STEP 3.

 

STEP 2Add/Create a Maps Activity

Now if you are not starting from scratch i.e creating a New Project in Android Studio. Then you need to add a Maps Activity in your Project.

  • To add a Maps Activity go to File -> New -> Activity -> Gallery. Select Google Maps Activity from the window that appears and click Next.

  • Give a name to the new Activity and Layout file. Click Finish. This will generate a Maps Activity for you with a layout file.

 

Some Important Points:

When Android Studio finishes building your Project after completing Step 1 or Step 2, there are some things that Android Studio has automatically done for you. Let us have a look at them in the following points.

  • First, if you open your app-level build.gradle file, you can see an entry implementation ‘com.google.android.gms:play-services-maps:15.0.1’ under dependencies. If not, then add it for yourself. This dependency states that this App uses Google Maps SDK distributed under Google Play Services.
  • Second, when the build is finished, Android Studio opens the google_maps_api.xml file in the editor. Notice that the google_maps_api.xml file contains instructions on getting a Google Maps API key before you try to run the application. We will discuss how to get Google Maps API key in Step 3. After you will get Google Maps API key, you have to paste the API key into the *string* element in the google_maps_api.xml file.
  • Third, open AndroidMaifest.xml file. There you can see multiple declarations created. The most important one is meta declaration under application element. This embeds the Google Maps API Key used in current Application.

  • Apart from this, you can add one more declaration under application element in AndroidMaifest.xml file given below. This declaration embeds the version of Google Play services that the app was compiled with.

  • There are some permissions automatically merged into your manifest. The below-given permissions are defined in the Google Play services manifest and are automatically merged into your app’s manifest at build time. You don’t need to add them explicitly to your manifest:
    android.permission.INTERNET – Used by the API to download map tiles from Google Maps servers.
    android.permission.ACCESS_NETWORK_STATE – Allows the API to check the connection status in order to determine whether data can be downloaded.

 

STEP 3Get a Google Maps API Key

Your application needs an API key to access the Google Maps servers. The key is free. You can use it with any of your applications that call the Maps SDK for Android, and it supports an unlimited number of users. There are two ways to get an API key, one is to use the link provided in google_maps_api.xml file created after Step 1 or Step 2. Or to visit Google Cloud Console and enter the credentials provided in google_maps_api.xml file. You will look at both methods one by one.

Use the link provided in the google_maps_api.xml file that Android Studio created for you

If you have created a Maps Activity using Android Studio Dialog Windows i.e Android Studio have created a google_maps_api.xml file, then this the method you should follow to get a Google Maps API key.

  • Copy the link provided in the google_maps_api.xml file and paste it into your browser. The link takes you to the Google Cloud Platform Console and supplies the required information to the Google Cloud Platform Console via URL parameters, thus reducing the manual input required from you.
  • Follow the instructions to create a new project on the Google Cloud Platform Console or select an existing project. Refer below given screenshots for help.
Created a New Project on Google Cloud Platform

Created a New Project on Google Cloud Platform

Create an API Key

Create an API Key

API Key Created

API Key Created

  • By following the above steps, an Android Restricted API key is automatically generated. So you don’t need to manullay restrict it. But if you want to see how to manually restrict an API key, then click on RESTRICT KEY in above Alert box.
Restrict API Key

Restrict API Key

  • Copy the resulting API key, go back to Android Studio, and paste the API key into the *string* element in the google_maps_api.xml file.

 

Visit Google Cloud Platform and create an API Key manuallly

While in the first method to get an API key, everything was pre-populated by the link provided in google_maps_api.xml file. In this method, you have to open Google Cloud Console in the browser and need to provide all the credentials on your own. You should follow this method if you already have an API key or Android Studio has not created a google_maps_api.xml file. Now follow the points below.

  • Go to the GoogleGoogle Cloud Console in your browser.
  • Create a new Project or use an existing one.
  • Go to APIS & Services -> Dashboard -> ENABLE API AND SERVICES and select Maps SDK for Android.
  • Now restrict the API Key for Android platform. You will need Package name and SHA-1 certificate fingerprint to restrict an API key for
    Android Platform. You can get Package name from AndroidManifest.xml file and to get SHA-1 certificate fingerprint read the last section of this post.
  • Copy the resulting API key, go back to Android Studio, and paste the API key into the *string* element in the google_maps_api.xml file.

 

NOTE:

You need an Account on Google Cloud Platform to get an API key. You will need a Gmail Id for creating an account on Google Cloud Platform, if not already.

 

STEP 4Review/Customize the Code

Android Studio automatically populates some code in both Maps Activity and Layout file when you create a Maps Activity using Android Studio. While this is enough to get you started, you can customize both Layout and Activity files as per your need. We will look at the code both in JAVA and KOTLIN.

  • In the basic code, we are getting a reference to Map Fragment and implemented OnMapReadyCallback interface and its method.
  • onMapReady() method adds a marker to the map with hard-coded coordinates. Refer the below-given code for your reference.
  • Throughout the whole tutorial series, we will look at the code both in JAVA and KOTLIN. It is your choice which language you want to use.






SHA-1 Certificate Fingerprint

To generate debug SHA-1 fingerprint on windows, go to cmd and type following command.keytool -list -v -keystore “%USERPROFILE%\.android\debug.keystore” -alias androiddebugkey -storepass android -keypass android

To generate release SHA-1 fingerprint on windows, go to cmd and then type following command.keytool -list -v -keystore your_keystore_name -alias your_alias_name. Replace your_keystore_name with the fully-qualified path and name of the keystore, including the .keystore extension.

 

NOTE:

  • Note that the API key is linked to the encryption key used to sign the APK.You need a different API key for each encryption key, including the release key that is used to sign the APK for publishing. You can define the keys for the debug and release targets in src/debug/ and src/release/.

 

With this, we have integrated a basic Google Map in an Android Application. Now you can run your Application on an Android device or an emulator. In the next post, we will add Location suppoert to the Android Map.


If you face any problem in the tutorial above, you can always leave a reply or reach me through the chat option given in the bottom right corner of the window.

Share this Story

Related Posts

Leave a Reply

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

About Me


I'm a college student pursuing B.tech. I created this website (Blog) to share my learnings with the people who search for the same content/knowledge.