Android Google Maps v2 Tutorial

android-map-v2-on-emulatorGoogle provides a service to integrate Map to Android App. Google Maps Android API, can be used to add maps to your application. The API provides functions to handle the access to Google Maps servers, data downloading, map display, and response to map gestures.

The latest release of the API “Google Maps Android API v2″, allows you to add maps into an activity as a fragment with a simple XML snippet.

Objective:

  • How to add Google Map v2 to Android application?

Environment & Tools:

( 1 ) Create Android Application

  • File >> New >> Android Application
  • App Name: Google Map v2
  • Enter Project Name: android-google-map-v2
  • Pakcage: com.hmkcode.android
  • Keep other default selections, go Next  till you reach Finish

( 2 ) Add Google Play Services Lib to the App

Refer to: Adding Google Play Services Library to Your Android App

( 3 ) Obtain Google Map v2 API Key

Refer to: Getting Android Google Maps v2 API Key

( 4 ) Create a Layout with Map Fragment

You can embed maps into an activity as a fragment with a simple XML snippet.

  • res/layout/activity_main.xml
<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=".MainActivity" >

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


</RelativeLayout>

( 5 ) Activity Class

  • /src/com/hmkcode/android/MainActivity.java
package com.hmkcode.android;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

( 6 ) Manifest XML File

In the manifest file we need to add the following

  • <use-feature> for OpenGL 2.0
  • <uses-permission> set of permissions
  • <meta-data> for API Key
  • <meta-data> for Google Play services version
  • /AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hmkcode.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
	<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    
    <uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
	<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
	
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.hmkcode.android.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>
        <meta-data
			android:name="com.google.android.maps.v2.API_KEY"
			android:value="AIzaSyCyzIXBgH0WPQGq9s9zG9gfWvvC...." />
        
        <meta-data android:name="com.google.android.gms.version" 
             android:value="@integer/google_play_services_version" />
    </application>

</manifest>

( 7 ) Deploy and Run

  • Emulator

To run this app on Android emulator, you need first to configure the emulator as explained in this post Configuring Android Emulator to Run Google Map v2

  • Actual Device

Otherwise, you can run it directly on actual device running Android 3.0+

android-map-v2-on-emulator