In the previous guide, we learned about how to get register and get Google Map API key. And In this guide, we assume that you have already created your new google map API key. This guide explains that you must use this article before you do anything on the map. Let us say that this is the must step to do.
The first thing is to add the Google Map Gradle dependency to the project, open
declare Google Map API key in your project manifest.xml inside the application tag like below.
Be sure to replace
Open
You must implement 

The first thing is to add the Google Map Gradle dependency to the project, open
build.gradle
and add line implementation 'com.google.android.gms:play-services-maps:16.1.0'
and synchronize your project. This will look like below:
build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
declare Google Map API key in your project manifest.xml inside the application tag like below.
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBMhY-xxxxxxxxxxxxxxxxxxxxxx" />
</application>
AIzaSyBMhY-xxxxxxxxxxxxxxxxxxxxxx
with your API KEY into value of android:value
. Open activity_main.xml
and declare MapView
like below:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.gms.maps.MapView>
</LinearLayout>
MainActivity.java
, initialize Google Map. Google Map has its features to handle the activity lifecycle for the following methods which will handle automatically as per activity state :- OnCreate()
- onPause()
- onResume()
- onLowMemory()
- onStop()
- onDestroy()
onStart()
and onExitAmbient()
as well, implement this as well if you need. Below is my MainActivity.java
MainActivity.java:
package queendevelopers.com.googlemapdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private MapView mapView;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
@Override
protected void onResume() {
mapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
mapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onStop() {
mapView.onStop();
super.onStop();
}
@Override
public void onLowMemory() {
mapView.onLowMemory();
super.onLowMemory();
}
}
OnMapReadyCallback()
if you're using Google Map V2 which overrides the OnMapReady
method. For more information on the life cycle of Google Map see Activity life cycle. We can see the Google Map on the phone now : 

Last edited: