I waited so long to write this blog, but due to lack of time, here I will discuss the secret behind interfacing (using) Google Maps V2 in your own Android Applications.

Steps involved

1) Maps belongs to Google, hence in order to use them in our application, we need to get respected support libraries from them. They (Google) placed these libraries in Google Play services. You can download and install the same in your Android Developer Tool (ADT) by navigating Window > Android SDK Manager and check Google Play Services and click Install packages …

1

2) And you have required libraries to use in your app, import the same libraries into your current workspace

You may find them at adt-bundle-linux-x86-XXXXXX/sdk/extras/google/google_play_services/libproject/google-play-services_lib

Note: don’t forget to check Copy projects into workspace

5

3) Create a New Android Application Project in Eclipse

4) Get SHA1 fingerprint of your ADT at Window > Android > Build

7.3

5) Get API_KEY

  1. Open browser type google.com and search for api console click the first link of your searchGoogle
  2. Sign in with your gmail accountapi
  3. Click Services and turn ONGoogle Maps Android API v2 from All Services7
  4. Click API Access > Create new Android key … 7.1
  5. You will get a pop up menu, enter your SHA1 fingerprint ; package name and then click Create7.4

6.  You will get an Api Key at the top, copy it for further procedure, thats it Sign out

7.5

6) Come back to your project, paste the API key at manifest of your project as below inside the <application></application> tag,

<meta-data android:name=”com.google.android.maps.v2.API_KEY” android:value=”AIzaSyCTUN9NUi3IRgIVncVwh-2R-WXa7JDABYE”/>

7)  Get the following permissions in manifest.xml

<uses-feature android:required=”true” 

android:glEsVersion=”0x00020000″/>

<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>

<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>

<uses-permission android:name=”com.wordpress.smdaudhilbe.mymapdemoo.permission.MAPS_RECEIVE”/>
<uses-permission android:name=”com.google.android.providers.gsf.READ_GSERVICES”/>

<permission android:name=”com.wordpress.smdaudhilbe.mymapdemoo.permission.MAPS_RECEIVE”></permission>

ACCESS_NETWORK_STATE – To check network state whether data can be downloaded or not
INTERNET – To check internet connection status
WRITE_EXTERNAL_STORAGE – To write to external storage as google maps store map data in external storage
ACCESS_COARSE_LOCATION – To determine user’s location using WiFi and mobile cell data
ACCESS_FINE_LOCATION – To determine user’s location using GPS
OpenGL ES V2 – Required for Google Maps V2

Finally your manifest.xml file should be like

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.wordpress.smdaudhilbe.mymapdemoo”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”11″
android:targetSdkVersion=”17″ />

<uses-feature android:required=”true” 

android:glEsVersion=”0x00020000″/>

<uses-permission android:name=”android.permission.INTERNET”/>

<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/> 

<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/> 

<uses-permission android:name=”com.wordpress.smdaudhilbe.mymapdemoo.permission.MAPS_RECEIVE”/>
<uses-permission android:name=”com.google.android.providers.gsf.READ_GSERVICES”/>

<permission android:name=”com.wordpress.smdaudhilbe.mymapdemoo.permission.MAPS_RECEIVE”></permission> 

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.wordpress.smdaudhilbe.mymapdemoo.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=”AIzaSyCTUN9NUi3IRgIVncVwh-2R-WXa7JDABYE”/>
</application>

</manifest>

8) Now we need to make Google Play Services project as a library to our project, right click over the project and click Build_Path > Configure Build Path …

on

Click Add and select the google-play-services_lib

9

Check Is Library and click Apply

check

again uncheck Is Library

uncheck

Now Navigate to Java Build Path > Libraries and click Add External JARs …

11

Select google-play-services.jar by navigating to current-WorkSpace-of-your-project/google-play-services_lib/libs/google-play-services.jar

pil

Then click Order and Export and check all options given below

checkall

and finally click OK …

Tired ???

9) fill activity_main.xml as

<?xml version=”1.0″ encoding=”utf-8″?>
<fragment
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/apk/res-auto&#8221;
android:id=”@+id/MYMAP”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
class=”com.google.android.gms.maps.MapFragment”/>

10) Let us jump to coding now, open up MainActivity.java and fill

package com.wordpress.smdaudhilbe.mymapdemoo;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

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

public class MainActivity extends Activity {

// Google Map
GoogleMap gMap;

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

// Single line to show Map
gMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.MYMAP)).getMap();
}
}

Thats it …! You may see a white blank screen at your emulator so thing like below

Eager to see map test your app in real device, you may customize it according to your wish, best reference for this is http://www.androidhive.info

I done this at Linux platform, hope this helps anyone in need

Enjoy … !

UPDATE : 

I found good resource for Google Maps V2 here

UPDATE: date : 09 / 01 / 2014

Here is the new Manifest permissions for any map to work for KitKat and before

<!– map required permission –>
<uses-feature
android:required=”true”
android:glEsVersion=”0x00020000″/>

<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>

<uses-permission android:name=”com.wordpress.smdaudhilbe.bquiet.permission.MAPS_RECEIVE”/>
<uses-permission android:name=”com.google.android.providers.gsf.permission.READ_GSERVICES”/>

<permission android:name=”com.wordpress.smdaudhilbe.bquite.permission.MAPS_RECEIVE”
android:protectionLevel=”signature”/>

And additional meta-data,

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

with old

<!– Map –>
<meta-data
android:name=”com.google.android.maps.v2.API_KEY”
android:value=”XXXXXXXX”/>

Your XML file should be,

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

<fragment
android:id=”@+id/MyMap”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
class=”com.google.android.gms.maps.MapFragment”/>

</RelativeLayout>

and finally class file,

// Google Map
GoogleMap gMap;

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

// get map
// gMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.MyMap)).getMap();
initializeMap();
}

// create map
private void initializeMap() {

if (gMap == null) {
gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.MyMap)).getMap();

// check if map is created successfully or not
if (gMap == null)
Toast.makeText(getApplicationContext(),”Sorry! unable to create maps”, Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onResume() {
super.onResume();
initializeMap();
}

Enjoy … !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s