Twitter LogoFacebook Logo
Implementing Google Maps Android
Learn how to implement Google Maps to your Android Applications
By: King

Hello, in this tutorial, I will show you how to add Google Maps to your Android application.

Add the Google Maps SDK

To begin, create a new Empty Android Application. Go to the app’s build gradle file and implement the Google Maps SDK. 

dependencies {
    ...
    implementation 'com.google.android.gms:play-services-maps:17.0.1'
}

Adding the Map to a Layout

Go to the activity_main.xml layout file. Switch into the Code view and add a fragment. 

Set the width, height, id, and the constraints. 

To turn this fragment into a Google Maps fragment, set the name attribute to "com.google.android.gms.maps.SupportMapFragment"

<fragment
        ...
        android:name="com.google.android.gms.maps.SupportMapFragment"/>

Loading the Map

Now that we have added the map to the layout, we need to load it up. Go to the MainActivity file and import the SupportMapFragment class. 

import com.google.android.gms.maps.SupportMapFragment;

In the onCreate() method, get the reference to the Google Maps Fragment in the layout using the fragment manager.  

Once we have the reference for the map, we need to attach a callback to it so we can know when the map is loaded onto the app. 


Import the OnMapReadyCallback.

import com.google.android.gms.maps.OnMapReadyCallback;

In the class header, implement the callback and then hover over on top of it. 


Press and hold on the Alt key and press enter.

Image from Codeible.com

Select the “Implement Methods” option and implement the onMapRready() callback method

@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
        
}

In the onCreate() method, grab the Google Maps fragment, call getMapAsync(), and pass in "this" for the callback. 

Since the class implemented onMapReadyCallback, we can pass in an reference to the class object in the getMapAsync() call.

The onMapReady() callback is triggered when the map is ready. We use this function to add contents to the map.

This is all we need to do to add the map to the app. The next thing we need to do is to generate an API key so we can use the Google Maps services.

Generating an API Key for Google Maps

Go to console.cloud.google.com. Sign in with your Google Account and create a new project. 

Image from Codeible.com
Image from Codeible.com

On the left, scroll down until you see the Google Maps Platform tab and click on it. 

Image from Codeible.com

In the content section, select the Maps SDK for Android option and enable the service. 

Image from Codeible.com

Once the service is enabled, click on the Credentials tab on the left, then click on “CREATE CREDENTIALS” at the top and select API key. 

Image from Codeible.com

Once the API key is created, copy it, and go back to Android Studio. 

Image from Codeible.com

In the manifests file, add a meta-data element inside the application and set the key.

If we run the app, we should see a map.

Image from Codeible.com

Possible Occurances

If your map is empty, close the app and reinstall the app by uninstalling it first on the emulator.

That is all for this video. If you find this video helpful, please give it a like, share, and subscribe to support the channel.


Sign In