Android Google Map v2 on Older Android Version

android-mapGoogle Maps v2 Android API, can be embedded into an activity as a fragment with a simple XML snippet in the layout file. Running Google Maps v2 on older version of Android “below API 11″ needs some support classes from the support library. Here we will see how to setup Google Maps v2 on Android 2.3.3.

 

 

 

 

Objective:

  • How to add Google Map v2 to older version of Android?

Environment & Tools:

  • Android Developer Tools (ADT) (or Eclipse + ADT plugin)
  • Samsung Galaxy S II with Android 2.3.3
  • Min SDK 8
  • Google Play Services revision 13
  • Android Support Library revision 19

( 1 ) Create Android Application

  • File >> New >> Android Application
  • App Name: Map v2 Android 2.2
  • Enter Project Name: android-older-version-map-v2
  • Pakcage: com.hmkcode.android
  • Keep other defualt 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

Map can be added to the layout as a fragment, for the older version of Android we will use SupportMapFragment.

  • /res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

( 5 ) Activity Class

  • Our activity class will extends FragmentActivity
  • /src/com/hmkcode/android/MainActivity.java
package com.hmkcode.android;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity  {

	@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="17" />

    <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="AIzaSyCSZ785FS597bMO.........." />

        <meta-data android:name="com.google.android.gms.version" 
             android:value="@integer/google_play_services_version" />
    </application>

</manifest>

UPDATE: you may need to copy google-play-services.jar to the project lib folder and add it to Java build path

Deploy and Run

android-map-v2