Android with Google Map v2


Google Map version 1 has been deprecated officially on 3rd December 2013. So this article aims to give knowledge about how to integrate Google Map v2 in android in Eclipse IDE. Refer Google Maps Docs for any further topic not covered in this tutorial.

1) Downloading google play services

Open Eclipse -> SDK Manager and in Extras section check whether Google Play Services are downloaded or not. If not then download first.

sdk manager



2) Importing Google play services into Eclipse

After downloading google play services we need to import it to Eclipse.
Go File -> Import -> Existing Android code into Workspace.
Click on Browse and select google play services folder from SDK folder as sdk -> extras -> google -> google_play_services (importantly select copy projects into workspace) and then finish.

3) Creating a new android project in Eclipse

Make an android project named MapTest. To know how to create new android project click here


new project

We need to use Google play services as a library so right click on the project and select properties. In Android, section select Add and select google-play-services_lib.
eclipse


4) Obtaining API key

a) Open Google API Console
b) Select Create Project
c) Name the project as MapTest and click create.


d) Select Use Google API and then Google Maps Android Apis.
e) Select Enable API.


f) Now select Go to credentials.
g) select google map API and Android in the first section and now it will ask you to add package name and fingerprint.
So again go to  Eclipse -> Window -> Preferences -> Android -> Build and copy SHA1 fingerprint.
Enter the following information in the console and click Create API key.


5) Add map key in manifest file

Add following permissions in manifest file

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

In this permission make sure to change package name

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

In application tag add following code

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

Now Manifest.xml file will be look like

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.praval.maptest"
    android:versionCode="1"
    android:versionName="1.0" >
    
     <permission
        android:name="com.praval.maptest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
 
    <uses-permission android:name="com.praval.maptest.permission.MAPS_RECEIVE" />
 

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    
    <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" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
         <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDD5fSxFq7ifUwvSKpBGb3FbGH1QyPJr30" />
    </application>

</manifest>


In activity_main.xml add following fragment

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.praval.maptest.MainActivity" >

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

</RelativeLayout>

 In MainActivity.java add following code

public class MainActivity extends Activity {
 
    // Google Map
    private GoogleMap googleMap;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        try {
            // Loading map
            initilizeMap();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
 
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
 
}

Now run the project and you will get map displayed.

Comments