Android SupportMapFragment Example - Truiton
Skip to content

Android SupportMapFragment Example

Android SupportMapFragment Example

Now days almost all Android apps have some sort of interaction with Google Maps Android API v2. This tutorial Android SupportMapFragment Example, is a part of my series of three on Google Maps Android API v2. In my previous tutorial on Android MapFragment, I mentioned a drawback of MapFragment, that it cannot be used on devices running Android API below version 12. The answer to this problem is Android SupportMapFragment. At Truiton I was developing an app in which we used Android SupportMapFragment class, so thought of sharing it with community. I was developing that app for Android Gingerbread  v2.3.3 i.e. API 10, but since MapFragment class cant be used, I used Android SupportMapFragment class.

The usage of Android SupportMapFragment is same as its sibling, the only difference is that, instead of using Activity class we would have to use FragmentActivity class. The reason for doing this is, we need to use a method getSupportFragmentManager(), and this method can be called in FragmentActivity class only.

In this Android SupportMapFragment Example I will not be explaining on how to generate a Google Maps Android API v2 key or how to create a project with Google Play Services SDK library. If you need to understand how to do these tasks please read my tutorial on Android MapFragment.

Android SupportMapFragment Example

I assume, by now you have a project set up with Google Play Services SDK and generated a Google Maps Android API v2 key, refer this article if you have not. Lets proceed with AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.truiton.supportmapfragment"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.truiton.supportmapfragment.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.truiton.supportmapfragment.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="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
     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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.truiton.supportmapfragment.SupportMapFragmentActivity"
            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="Input-API-Key-Here" />
    </application>

</manifest>

lets discuss the marked lines in above Android SupportMapFragment Example manifest. In lines 7-9 you can see minimum sdk version 8 is specified as this is an Android SupportMapFragment example. Next in lines 11-13 we are defining a permission with our app package to access Google Maps Android API v2, and using the same in line 19. To display Google Maps on your device please specify your API key at line 48.

Next lets have a look at the layout file:

<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=".SupportMapFragmentActivity" >

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

</RelativeLayout>

Here in the activity_support_map_fragment.xml I defined a fragment with class com.google.android.gms.maps.SupportMapFragment, this fragment is used to display Android SupportMapFragment.

The main class which displays map is :

package com.truiton.supportmapfragment;

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

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;

public class SupportMapFragmentActivity extends FragmentActivity {

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

 FragmentManager fmanager = getSupportFragmentManager();
        Fragment fragment = fmanager.findFragmentById(R.id.map);
        SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
        GoogleMap supportMap = supportmapfragment.getMap();

 }

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

}

In this SupportMapFragmentActivity.java, I extended FragmentActivity as we are using Android SupportMapFragment. And as I stated before to use Android SupportMapFragment class we need to call getSupportFragmentManager() method. Therefore we need to extend FragmentActivity class.

By now you might have guessed that its pretty much same as Android MapFragment. Therefore I’d like to conclude the tutorial Android SupportMapFragment Example, and conclude my series on Google Maps Android API v2. In this tutorial I gave an overview of Android SupportMapFragment class and explained how to display Google Maps on Android device with an API version lower than 12. Hope this helped you, if it did please like and share this on Google+ and Facebook also like our Facebook page for updates.

7 thoughts on “Android SupportMapFragment Example”

  1. Hi,
    I also used the same SupportFragmentManager but im getting a error in the line setContentView(R.layout.main); it says
    NoClassDefFoundException.
    I have already added google support Library.
    Pls help me

    1. Hey,

      Are you getting this error on line getMenuInflater().inflate(R.menu.support_map, menu); as this a menu xml. You need to add an xml file by the name of support_map in res/menu folder, or you can remove this method all together.

  2. Great tutorial but… android.view.InflateException: Binary XML file line #13: Error inflating class fragment

    android.view.InflateException: Binary XML file line #13: Error inflating class fragment
    at com.quexjh.cfeapp.MapActivity.onCreate(MapActivity.java:26)

    the line 26 is setContentView(R.layout.activity_support_map_fragment);

    help please!!

      1. Hello Aron, Senou

        Thanks allot for pointing it out. Seems like I missed out an element which was required in the application element of manifest. Now corrected.

        Please check the manifest code in tutorial above.

        Thanks

Leave a Reply

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