How To Integrate Google Maps In Android Applications

This blog briefs the pictorial as well as theoretical information about integrating Google Maps in Android app.

Google Maps

 
According to Wikipedia Google maps provides satellite imagery, street maps, 360° panoramic views of streets, real-time traffic conditions and route planning. So if you want to add any feature related to maps in your app, you may use Google Maps API.
 

Prerequisites for Using Google Maps API

Google Developers Console account

Register on Google Developers Console using your Gmail Id. It's required for getting the Google Maps API key.
 

How to get Google Maps API access key

 
Enable Google Maps SDK
  • Login to Google developers console. Click on "New Project" and fill in the required information
  • Click on "Create"
How To Integrate Google Maps In Android Application
  • From the side menu, go to APIs & Services -> Dashboard
  • Click ENABLE APIS AND SERVICE
  • Search and select Maps SDK for Android
  • Click on Enable
 
How To Integrate Google Maps In Android Application
How To Integrate Google Maps In Android Application 

After enabling Maps SDK, you now need API key credentials for integrating into your project.

How to get API key

  • From side menu, go to APIs & Service -> Credentials
  • Click on Create Credentials -> API key
How To Integrate Google Maps In Android Application

A dialogue box containing your API key will now appear. Keep your API key 'very' private and do not share with anyone. You are now ready to create an app having Google Maps.

Integrating Maps in Android Studio Projects

  • Start a new project in Android Studio.
  • While adding activity, choose Google Maps Activity. This will add all important items needed for Maps integration in your app. 
A file named google_maps_api.xml will be generated in values (debug) folder. You need to place your API key here.
  1. <resources>  
  2.    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">PLACE_YOUR_KEY_HERE</string>  
  3. </resources>  

This key is then added in Android.manifest as meta data. You will find the below code in your manifest for this purpose (Note: If you are manually adding Google Maps Activity in your project, then you need to add this meta data yourself),

  1. <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" />  

Run your app. You will see Google Maps Activity, with Maps opened in your application.

Code Explanation

Google Play Services (Maps Services) Plugin

For accessing Google Maps in your application, Google Play services plugin is needed in your app's build.gradle file. As you had added Google Maps Activity, Android Studio had added this plugin itself (Note: If you are manually adding Google Maps Activity in your project, then you need to add this dependency yourself)

  1. dependencies {...  
  2.     implementation 'com.google.android.gms:play-services-maps:16.0.0'...  
  3. }  

Permissions

For accessing location, ACCESS_FINE_LOCATION permission is needed in your manifest. You will find the below permission in your manifest file after adding Google Maps Activity in your project. (Note: If you are manually adding Google Maps Activity in your project, then you need to add this permission yourself),

  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />   
 Layout

According to Android documentation, a Map Component fragment is the simplest way to add maps in an app. Fragment adds a wrapper around the map layout for handling the necessary life cycle events of a map. This component can simply be added in layout as follows,

  1. <fragment xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    xmlns:map="http://schemas.android.com/apk/res-auto"  
  3.    xmlns:tools="http://schemas.android.com/tools"  
  4.    android:id="@+id/map"  
  5.    android:name="com.google.android.gms.maps.SupportMapFragment"   
  6.    android:layout_width="match_parent"  
  7.    android:layout_height="match_parent"  
  8. tools:context=".MapsActivity" />   
As we had added Google Maps Activity provided by Android Studio by default, so this layout was automatically added in main activity's layout. (Note: If you are manually adding Google Maps Activity in your project, then you need to add this fragment yourself)

Activity

As you have added Maps Fragment in your layout, so you need to extend your Activity with FragmentActivity and implement OnMapReadyCallBack interface. For initialization, your map (with id map in layout) must be acquired using getMapAsync(OnMapReadyCallback). Below is the example Activity having map. (Note: This will be added automatically by Android Studio, when you add Google Maps Activity in your project)

  1. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {  
  2.     private GoogleMap mMap;  
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);  
  7.         mapFragment.getMapAsync(this);  
  8.     }  
  9.     @Override  
  10.     public void onMapReady(GoogleMap googleMap) {}  

In onMapReady Function, you may add remaining required functionalities of Maps. All the available functions are available in Android's Map Fragment documentation.

Final Words

I am adding a sample project to my Blog post. Please update your API_KEY in the project. In case you have any query regarding maps, feel free to comment. Give a thumbs up if you find my blog helpful. Happy coding :)