Android Google Map V2 Integration

By | February 18, 2016

If you are developing an android application which is location based , where you want to show map with locations marked and also different routes on the map , google map V2 is the best approach for it. implementing google map v2 is very easy , in this tutorial we see a quick start to adding a map to an Android app. Android Studio is the recommended development environment for building an app with the Google Maps Android API .Google provides facility using google play services library which you can sync thru gradle script .

androidgooglemapv2

 

Getting Google Map API Key

In order to create Google Map API , you must register your application at Google Developer Console , enable API and then create api key Follow Below Screenshot how to get map api key .

1 Create a Project

1.a
Visit “https://console.developers.google.com/project” and click “Create Project” Button .

Android Google Maps

1.b
A Pop window comes up ,in project name field enter the project name.

GoogleMaps

2. Enable Map API

2.a
After Project creation you will a pop which ask for “enable and manage APIs” click on it

GoogleMaps

2.b
Here you will see a list of google api , click on “Google Map Android API”

GoogleMaps

2.c
Click “Enable API”

GoogleMaps

3 Create Android API Key

3.a
In the left panel click on a link with Text “Credentials”

GoogleMaps
3.b
Under “Create Credentials” select “API Keys”

GoogleMaps

3.c
A popup comes with four buttons, where click on “Android key”

GoogleMaps

3.d

Here is the important step

You need to get a generate SHA-1 fingerprint using java keytool because you have to provide it to google console in order to get your API key for map.Open your terminal and execute the following command to generate SHA-1 fingerprint.

Syntax
keytool -list -v -keystore “%USERPROFILE%\.android\debug.keystore” -alias androiddebugkey -storepass android -keypass android

Example
keytool -list -v -keystore C:\Users\pawan\.android\debug.keystore -alias androiddebugkey -storepass android -keypass android

Copy the generate SHA-1 fingerprint.

GoogleMaps Sha1Key

3.e

Enter the application package name in package name field .
Copy Paste the above generate SHA-1 fingerprint in SHA-1 fingerprint field .

GoogleMaps
3.f

when you click create button you will get the Google MAP Key , and this key we will use in our application.

GoogleMaps
Android Studio Project

In android studio click on File > New Project you will see a “Create New Project” window where enter you Application name and Package and click next In the next window you will see a list of activity there select “GoogleMapsActivity” and then click next .

GoogleMaps androidstudioproject2

androidstudioproject2
Build Gradle

open the gradle file , add below code. and sync.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.google.android.gms:play-services:8.4.0'
}

Placing Google Map Key

Place Google Map Key in a XML file as show in below file path.
src/debug/res/values/google_maps_api.xml

<resources>
    <string
        name="google_maps_key"
        templateMergeStrategy="preserve"
        translatable="false">YOUR_KEY_HERE
    </string>
</resources>

Android Manifest

In the manifest file add the meta data tag in with a attribute android:value takes the “Google API Key” and add ACCES_FINE_LOCATION Permission .

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Create a XML Layout with file “activity_maps.xml” ,which contains a Fragement Tag with attribute “android:name” whose value points to “com.google.android.gms.maps.SupportMapFragment”

file : activity_maps.xml

<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="com.tutorialsbuzz.androidgooglemaps.MapsActivity" />

Map Activity

Create a Class MapsActivity which extend FragementActvity and implements onMapReadyCallback.

Inside the onMapReady() method set the “map type” , add the marker on map with markeroption object by specifying lattitude and longitude ,title ,icon move the camera towards marked location.

file : MapsActivity.java

package com.tutorialsbuzz.androidgooglemaps;

import android.support.v4.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);
        // 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;

        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        // Add a marker in Delhi and move the camera
        LatLng delhi = new LatLng(28.38, 77.12);
        MarkerOptions markeroptions =new MarkerOptions();
        markeroptions.position(delhi);
        markeroptions.title("Marker in Delhi");
        
        mMap.addMarker(markeroptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(delhi));

    }
}

Run the Application

Android Map Type

Google Maps V2 provide following Map Type .

1. MAP_TYPE_NORMAL
Basic maps.

mMaps.setMapType(mMaps.MAP_TYPE_NORMAL);

AndroidGoogleMaps

2.MAP_TYPE_SATELLITE
Satellite maps with no labels.

mMaps.setMapType(mMaps.MAP_TYPE_SATELLITE);

AndroidGoogleMapsetallite

3.MAP_TYPE_HYBRID
Satellite maps with a transparent layer of major streets.

mMaps.setMapType(mMaps.MAP_TYPE_HYBRID);

AndroidGoogleMapshybrid

4. MAP_TYPE_TERRAIN
Terrain maps.

mMaps.setMapType(mMaps.MAP_TYPE_TERRAIN);

AndroidGoogleMapsterrain

5.MAP_TYPE_NONE
No base map tiles.

mMaps.setMapType(mMaps.MAP_TYPE_NONE);
  • hi , i loved your tutorial its really informative ..
    Please can you assist in i have a problem with my maps you see, i have done everything as prescribed by several tutorials on how to implement the pas in my app but still when i upload the apk to google play the maps still wont load but before i upload it works in debug mode.my stack overflow i get is that wrong credentials but the credentials i used they are very correct.Please help me i have been all over the net looking for a solution i am very stuck.