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:

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:

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 :

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.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

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

7 thoughts on “Android SupportMapFragment Example

  • Lokesh

    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

    • Mohit Gupt Post author

      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.

  • Aaron

    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!!

      • Mohit Gupt Post author

        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