Android MapFragment Example - Truiton
Skip to content

Android MapFragment Example

Android MapFragment Example

Google rolled out its second version for maps API on android called Google Maps Android API v2. As I introduced Google Maps Android API v2 in my previous article, in this Android MapFragment Example tutorial I will be following up with an introduction to Android MapFragment class by an example. At Truiton we were working on an Android app which was using Google Maps, hence I thought of writing a tutorial about it.

Earlier to use Google Maps on Android we had to extend MapActivity class. But for new Google Maps Android API v2, we have to use Android MapFragment class, which could be instantiated in any Activity. This poses as a great advantage over version 1 of this API. Since this tutorial focuses on Android MapFragment Example, we would not be discussing first version of this API.

Creating a project with Google Play Services SDK library

As Google Maps Android API v2 is bundled in Google Play Services SDK, therefore to show Google maps on android we need to include Google Play Services Library in our project. To do so please follow steps below :

  1. Open Android SDK Manager -> Extras -> Google Play Services -> Install Packages.
  2. In Eclipse File-> Import -> Android -> Existing Android Code into Workspace -> Browse to your Android SDK Folder -> /Extras/google/google_play_services/libproject -> Finish.
  3. Create new android project -> After creating project right click on its folder and select properties.
  4. Goto “Android” property -> click add button below -> select “google-play-services_lib” -> hit Ok and you’re all set.

Now that we have a project with Google Play Services Lib included, we can move on to Android MapFragment part of it. To display maps in a MapFragment, many permissions are to be granted to the application. But since Google Maps on Android are in a form of an API, before granting permissions we have to generate an API access key.

Generating a Google Maps Android API v2 Key

To Access Google Maps Android API v2 by Android MapFragment Example we need to create an Android API key via Google API Console. The steps for doing so are:

  1. Login to API console
  2. Create a Project -> Goto Services -> Turn on the switch for Google Maps Android API v2
  3. Goto API Access -> Create new Android Key-> In popup enter one SHA1 certificate fingerprint and package name (separated by a semicolon) per line.
  4. To get SH1 certificate of your app open command prompt, navigate to your Java bin folder and enter this command:
  5. keytool.exe -V -list -alias androiddebugkey -keystore "C:\Users\ABC\.android\debug.keystore" -storepass android -keypass android
  6. From here copy Sh1 certificate and also copy your package name from eclipse
  7. Now enter these values in the API Console popup in the desired format, you key will be generated.

 Android MapFragment Example

As of now we have fulfilled all the prerequisites to display Google Maps Android API v2 in Android MapFragment. So lets start with app manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="17" />

        android:protectionLevel="signature" />

        android:required="true" />

    <uses-permission android:name="com.truiton.mapfragment.permission.MAPS_RECEIVE" />
    <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="" />
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            android:value="Input-API-Key-Here" />


In this Android mapfragment example manifest have a look at highlighted lines 11-13 and 19, here I have defined a custom permission and used it. As you might remember from steps above, we mentioned our package in Google API console. Therefore to receive maps from API we need to create a permission with our package i.e. com.truiton.mapfragment. In your app just change this package name in permission to you app’s package name to make this work. Later in this Android mapfragment example manifest line 48 you need to specify you API key.

Moving on to layout where Android MapFragment would be placed:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:layout_height="fill_parent" >

        android:layout_height="match_parent" />


As you can see here I have added a fragment tag which calls .

Since this is a beginner tutorial for Android MapFragment, in the main activity below I have not called any google maps methods to modify it:

package com.truiton.mapfragment;

import android.os.Bundle;
import android.view.Menu;

public class TruitonMapFragmentActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(, menu);
		return true;

Android MapFragment

With this I can conclude my tutorial on Android MapFragment Example. This tutorial was my second post in the series of three about Google Maps Android API v2. In my previous article I gave an introduction to Google Maps Android API v2. My next tutorial would be about Android SupportMapFragment. As the MapFragments have a drawback i.e. they are not compatible with devices running android API lower than 12. Hope this tutorial helped you out, if it did please like and share this with your friends on Google+ and Facebook, also please like our Facebook page.

4 thoughts on “Android MapFragment Example”

  1. Hi, i liked ur post although i think it would be nice if u would show an example like a picture or something like it so people can see if that’s what they want.

  2. Thanks for this great tutorial. I have a question:
    Since API_KEY is hard coded in the Android Manifest, How to release the application with a Release API KEY? I can use the DEBUG API KEY to run the project in ADT. When I export to apk package, it can be installed on the device but it cannot show the map. Does it mean I need to use my own Keystore to create the SHA1, then get teh API KEY for the release and hard coded it to the manifest? What is the proper way to release this application? Thanks!

    1. Please follow the steps for Generating a Google Maps Android API v2 Key and then use it while releasing your APK. And if you are creating an apk for play store then first you need to create a keystore for it and then use its SHA1 to generate a release key.

Leave a Reply

Your email address will not be published. Required fields are marked *