Home > Android, Computer/Technology, Programming > Google Maps v2 Integration on Android

Google Maps v2 Integration on Android

Integrate Google Maps API into a new Android Project

This post will show you how to integrate the Google Maps v2 in a new Android Project. I’ve learnt that without proper guidance, it can be quite daunting to do. I had to dig in several posts to be able to get it to work properly and more importantly, understand every concept and steps. You should have some basic Android knowledge to be able to create it; i’ll try to be as detailed as possible to explain the integration.

First of all, create a new Project in Eclipse. I named my Project “GoogleMapsAndroidV2” and package name “com.example.googlemapsandroidv2“. Note your package name somewhere, you will need it afterwards.

To include the Google Map, you will need the Google Play Services library (google-play-services_lib). To download it, open your SDK Manager, scroll down to Extras and only select Google Play Services. Afterwards, click install package.

Untitled5

You need to Import Android Project from existing code. When choosing the project, navigate to your SDK Folder and select extras\google\google_play_services\libproject\google-play-services_lib (mine is C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib).

Now, you are ready to add google-play-services_lib library to your project. In the left pane (Package explorer), select the project root folder – in my case GoogleMapsAndroidV2 and right click -> properties OR press ALT+ENTER key.

Select Android on the left pane, and on the right pane, Click the Add button. Select google-play-services_lib and press OK, then apply button.

Untitled4

Now to integrate Maps into your newly created Android Project, your first step is to create a new project in the Google API console. You need to create a new project here for every project that uses the Android Play Services API.

After creating a new project click on the APIs & auth and enable Google Maps Android API v2.

Untitled

At this point, you will need to include your SHA1 fingerprint to create a new key to register with your Map project in play console. In Eclipse go to Window (menu)-> Preferences -> Android -> Build. Copy your SHA1 key. 

Untitled7

Now back in the play console, you will need to go to Credentials and click on create new key button and choose Android Key. What is the purpose of the key? “Each request to an API that is represented in the Google Developers Console must include a unique identifier. Unique identifiers enable the Developers Console to tie requests to specific projects in order to monitor traffic, enforce quotas, and handle billing.“. For more info click here.

Enter your SHA1 certificate fingerprint concatenated to your package name (seperated by a semicolon “;”).

<SHA1>;<PACKAGE_NAME>

My package name is “com.example.googlemapsandroidv2“.

Untitled2

Note your generated API Key – you will need this key to include in your application.

Untitled3

 

In Eclipse, open your AndroidManifest.xml and enter the following meta-data under the application tag:

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

Change the value of the API_KEY to the one you generated.

You will need a bunch of permissions in your AndroidManifest.xml:

    <uses-permission android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Open your activity layout file and enter the following code snippet:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Finally open the associated activity class file and enter the following code:

package com.example.googlemapsandroidv2;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;

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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends Activity {

	private static final LatLng MY_HOME = new LatLng(-20.2615011107153, 57.490380056682);
	private MapFragment mMapFragment;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
		GoogleMap map = mMapFragment.getMap();
		map.setTrafficEnabled(true);
		CameraPosition cameraPosition = new CameraPosition.Builder().target(MY_HOME).zoom(17).bearing(90).tilt(30).build();
		map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		map.setMyLocationEnabled(true);
		map.setIndoorEnabled(true);
	}
}

Note that MY_HOME constant contain the location of my home coord, so change to your Lats, Longs.

Test your application on your device and Voila! 🙂 Now you can have fun adding your markers, circles, rectangle, squares, including geofencing in your maps etc.

device-2014-01-17-193011

Hope you enjoyed my guide, feedbacks are welcomed.

//roshans89

Advertisements
  1. Mitesh Summun
    January 17, 2014 at 11:33 pm

    nice I must say. am impressed.

  2. Coder
    April 6, 2014 at 1:53 pm

    Nice tutorial! Can you provide the project for download, pls?

  1. No trackbacks yet.

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

%d bloggers like this: