How to Add the Google Map to the Android Studio Project

Hey Developers, today we are going to learn “how to add the google map to the Android studio project”. We are complete this project in 6 steps.

"how to add the google map to the Android studio project"

Let’s get started…

1- Download and Install Android Studio

First, Download & Install Android Studio. If you don’t know how to download & install Android Studio, So click this link and read Install & Setup Android Studio, Java JDK & SDK.

2-Create New Project with Google Map Activity. Follow step by step.

1- Start the Android Studio.
2. Create a new project.
3- Choose the project platform. Like Phone and Tablet & more.
4- Select Google Maps Activity, then click Next.

5- Enter the:

-App Name

-Package Name

-Project location

-The select programming language Java or Kotlin and the minimum API level supported by your app.

6- Click Finish.

How to Add the Google Map to the Android Studio Project


3-Need an API key to access the Google Maps servers

The key is free. So you have to need an API key with restriction for Android apps. You can use it with any kind of applications and it supports an unlimited number of users.

i- Go to the Google Cloud Platform Console.
ii- Create a new project
iii-Copy API Key and paste by default create google_maps_api.xml in your project (res/values/google_maps_api.xlm).

API key to access the Google Maps servers How to Add the Google Map to the Android Studio Project

4-  Defines XML file in the Map layout

When we create google map activity. The XML file that by default defines the app’s res/layout/activity_maps.xml layout. It contains the following code:

<?xml version="1.0" encoding="utf-8"?>
<fragment 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />

5- Map Activity Java File

Java file that by default defines the maps activity is named MapsActivity.java.

package com.techpassappmaster.googlemapdemo;

import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
       
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

 
        LatLng new_delhi = new LatLng(28.613939, 77.209023);
        mMap.addMarker(new MarkerOptions().position(new_delhi).title("New Delhi"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(new_delhi));
    }
}

6- Run & Test App

Click the Run menu option to run your app.

Happy Learning!!!  🙂

Leave a Reply