Google Map V2 Step By Step Example

Please follow and like us:

google map v2

Step 1: Create New Project In Android Sudio for google map.

  • Create new project as File > New Project
  • Add your application name as “GoogleMapV2”
  • Add your company domain as “com.nkdroid”
  • Press Next
  • Add your Minimum SDK as API 2.2
  • Add Blank Activity to your application
  • Add activity name, layout name and title of activity.
  • Press Finish

Google Map V2 Step By Step Example

 

Step 2: Add Google Play Service Library for google map

  • Go to File > Project Structure and select “app” folder under modules
  • Now select Dependencies tab
  • Press “+” button and select “Library Dependency”
  • Select Play service Library and press Ok
  • Press Apply and Ok to save changes.

Step 3: Generate Keystore

  • Go to Build > Generate Signed APK
  • Press “Create New”
  • Add keystore path location and other required field (keystore path used for generate SHA 1 fingerprint)
  • Press Next
  • Add APK path Location
  • Press Finish

Step 4: Generate SHA 1 Fingerprint

  • Open your terminal (CMD) from windows or select Tools > Open Terminal from android studio
  • Execute the following command to generate SHA-1 fingerprint.
  • keytool.exe -list -v -keystore “add path of key store” -storepass your password -keypass your password
  • Press Enter and copy generated SHA 1 fingerprint.

Step 5: Now Open Google API Console

  • Create new Project
  • Go to API & auth > APIs
  • Enable Google Maps Android API v2
  • Go to API & auth > Credentials
  • Press “Create New” button and generate new android key
  • Add SHA1;your_package_name and press “Create”
  • Copy generate android API key

 

Step 6: Add The Map Key In The Manifest File

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

    <permission
        android:name="nkdroid.com.googlemapv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="nkdroid.com.googlemapv2.permission.MAPS_RECEIVE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

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

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAQt6GlX05DEsWJI8Un5A7o_euIkKOT21M" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 7: Add The Following Code To Your Layout File for google map

<RelativeLayout 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=".MyActivity">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

Step 8: Add The Following Code To Your Activity Class

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MyActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        // Get a handle to the Map Fragment
        GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        LatLng india = new LatLng(22.3000, 73.2003);
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(india, 13));
        map.addMarker(new MarkerOptions()
                .title("Vadodara")
                .snippet("Beautiful City")
                .position(india));
    }
}

This tutorial is about to implement Google Map V 2.0 in Android Application. Let me know if any issue during implement this example.

nirav kalola