Android Google Maps

Android provides facility to integrate Google map in our application. Google map displays your current location, navigate location direction, search location etc. We can also customize Google map according to our requirement.

In order to use the Google Maps API, you must register your application on the Google Developer Console and enable the API.


Step 1: First of all sign in Google Developer Console and create your API Key


Your newly created API Key looks like as shown below


Step 2: Once you create API Key we recommend to restrict your API Key so that only and only your application use it.


To get your Debug Certificate Fingerprint from Android Studio follow instruction as shown in image below


Create Project

So well done guys everything is ready, so we will create Google Map Android Project. Now choose Google Map Activity from project template.


In next step you will get a auto create project and structure of that project will looks like below image. And in this project structure there are two folder's i.e. debug and release. Both contain res/values/google_maps_keys.xml.

Now what you have to do with these two google_maps_keys.xml. In debug folder file you put your API Key generated for development purpose only and in release folder file put your API Key. You can put same API Key in both folder.


In next step check for permission and meta-data tag in AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.theitbulls.googlemapex">

    <!-- This permission is requiredt for Google Maps Application -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <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">

	<!-- Make sure below meta-data must be in your AndroidManifest.xml -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        ...

    </application>
</manifest>

In next step we create MapsActivity

package com.theitbulls.googlemapex;

import android.os.Bundle;

import androidx.fragment.app.FragmentActivity;

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);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


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

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}


Now create layout/activity_maps.xml containing map fragment

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    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" />