Google Maps in Android Application with new Google Maps Android API v2 using SupportMapFragment

January 2, 2013
By

In this article, we will develop an Android application containing Google Maps using new Google Maps Android API v2 which is released on 03-Dec-2012.



This application is developed in Eclipse 4.2.1 with ADT plugin ( 21.0.0 ) and Android SDK ( 21.0.0 ) and is is tested in a real Android Phone with Android version ( 2.3.6 ).



1. Download and configure Google Play Services Library in Eclipse

Google Map for Android is now integrated with Google Play Services. So we need to set up Google Play Service Library for developing Google Map application in Android.

Please follow the given below link to setup Google Play Service library in Eclipse.

http://developer.android.com/google/play-services/setup.html


2. Create a new Android Application Project namely “LocationGoogleMapV2Demo”

Create a new Android Application Project

Figure 1 : Create a new Android Application Project


3. Configure Android Application Project

Create a new Android Application Project

Figure 2 : Create a new Android Application Project


4. Design Application Launcher Icon

Design application launcher icon

Figure 3 : Design application launcher icon


5. Create a blank activity

Create a blank activity

Figure 4 : Create a blank activity


6. Enter Main Activity Details

Enter Main Activity Details

Figure 5 : Enter Main Activity Details


7. Link to Google Play Service Library

Link this project to Google Play References Library

Figure 6 : Link this project to Google Play References Library


8. Get the API key for Google Maps Android API v2 

We need to get an API key from Google to use Google Maps in Android application. Please follow the given below link to get the API key for Google Maps Android API v2.

https://developers.google.com/maps/documentation/android/start


9. Add Android Support library to this project

By default, Android support library (android-support-v4.jar ) is added to this project by Eclipse IDE to the directory libs. If it is not added, we can do it manually by doing the following steps :

  • Open Project Explorer by Clicking “Window -> Show View -> Project Explorer”
  • Right click this project
  • Then from popup window, Click “Android Tools -> Add Support Library “

10. Update the file AndroidManfiest.xml

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

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

    <permission
        android:name="in.wptrafficanalyzer.LocationGoogleMapV2Demo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-permission android:name="in.wptrafficanalyzer.LocationGoogleMapV2Demo.permission.MAPS_RECEIVE"/>

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="in.wptrafficanalyzer.locationgooglemapv2demo.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="YOUR_API_KEY"/>

    </application>
</manifest>

Note : In the above code, replace “YOUR_API_KEY” with the api key, you generated in step 8.


11. Update the layout file to display Google Map using SupportMapFragment

<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"
    tools:context=".MainActivity" >

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>

12. Update the file src/in/wptrafficanalyzer/locationgooglemapv2demo/MainActivity.java

package in.wptrafficanalyzer.locationgooglemapv2demo;

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

public class MainActivity extends FragmentActivity {

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

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

13. Running the application

Screenshot of the application in execution

Figure 7 : Screenshot of the application in execution


14. Download Source Code


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , ,

50 Responses to Google Maps in Android Application with new Google Maps Android API v2 using SupportMapFragment

  1. Techi on January 10, 2013 at 11:43 pm

    Hi George, Thanks for the tuts. Can you tell me how you ran the map on emulator?? I followed the link ” . . . ” and installation were successful but when I ran your program I got a screen with “this app won’t run unless you update google play services” text. Any vision you would like to throw how you ran it?
    Thanks in advance.

    • george on January 11, 2013 at 5:50 am

      Hi,
      Ideally, Google Map Android API V2 cannot run in emulators. Because, Google Map Android API V2 is integrated with Google Play Services and is not supported on the Android emulator.

      • Techi on January 11, 2013 at 10:36 am

        Thank you for such instant reply. Even I tried to run on my device but ends with force close. Haven’t tried in debug mode. I’l be glad if you can you suggest the proper way to run it and view the map.

    • Amit Bhaliya on November 16, 2013 at 7:18 pm

      When i add this code it will generate the error message like “Google Map Can not be resolve type”

  2. Sanket jain on January 19, 2013 at 6:54 pm

    I am trying to work with the example u have provided and followed every step which u have mentioned out here.. I am having problem with the 7th step where you have showed image in which you link the google play service library with the project when i am trying that i am not getting the library in the second part where you have marked islibrary.and when i run the application i am getting layout inflator error pasting the error for your reference.

    01-19 18:47:34.875: E/AndroidRuntime(15640): FATAL EXCEPTION: main
    01-19 18:47:34.875: E/AndroidRuntime(15640): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
    01-19 18:47:34.875: E/AndroidRuntime(15640): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
    01-19 18:47:34.875: E/AndroidRuntime(15640): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
    01-19 18:47:34.875: E/AndroidRuntime(15640): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279)

    • george on January 20, 2013 at 12:49 pm

      Hello Sanket,
      Please ensure that, you have successfully installed and imported the Google Play Services library into the Eclipse Workspace.
      If the library is imported successfully then it will be listed in the Project Explorer View.

  3. Jorge Andrade on February 5, 2013 at 3:53 pm

    I tried running your app on my Samsung galaxy S, version 4.0.4, even though the app runs I get a blank white screen with the zoom buttons(the “+” and “-”), but no map at all. I’m kinda new to this whole Google API, any help that you can provide will be very appreciated :) Thanks for the tutorial you made it look incredibly simple.

    • george on February 5, 2013 at 5:26 pm

      Please ensure that, the correct API key is entered in the AndroidManifest.xml.

      • Jorge Andrade on February 5, 2013 at 6:35 pm

        I kept checking that, and constantly made new ones and reused them on the Manifest. I’m starting to question my android device. :s
        My Project’s manifest is

        and my device version is 4.0.4, could this cause a problem with the Google API v2.0 ?

        • Jorge Andrade on February 5, 2013 at 6:37 pm

          rewriting what didn’t show up:
          “My Project’s manifest is
          android:minSdkVersion=”15″
          android:targetSdkVersion=”16″
          and my device version is 4.0.4, could this cause a problem with the Google API v2.0 ?”

          • Gilgaretch on March 10, 2013 at 5:28 am

            Did you ever resolve this Jorge? I’m having the same exact issue and it’s driving me batty. Rechecked my API key a dozen times, and recreated it several times as well.

          • AJ on April 21, 2013 at 10:41 pm

            Anybody resolved this yet? I am facing this issue since a week, white screen with the zoom buttons(the “+” and “-”), but no map at all.

          • george on April 21, 2013 at 10:54 pm

            Once you entered the correct Android API key, please clean ( Project -> Clean ) your project from Eclipse IDE before running the application.

          • AJ on April 22, 2013 at 12:00 am

            Thanks for you quick reply George :)
            I have resolved my issue, issue was wrong map keys.
            Wrong service was enabled. Make sure the “Google Maps Android API v2″, not “Google Maps API v2″ is enabled and re-generate the API key.

            Thanks a LOT GEORGE The GREAT!! :)

  4. naz on February 10, 2013 at 6:19 pm

    I am getting a “Binary XML file line #14: Error inflating class fragment” error. Kept getting that and driving me crazy. The device I am testing it with is Gingerbread 2.3.6 Samsung Galaxy Y with Googleplay.

    • Chris on February 22, 2013 at 3:59 pm

      I’m getting the same error.

      I’ve double checked that play services are properly imported. I downloaded the source code directly.

      • george on February 25, 2013 at 7:47 am

        Please ensure that, the class MainActivity extends FragmentActivity instead of Activity class.

  5. Tony Green on February 25, 2013 at 10:25 pm

    Thank you SO MUCH for this!

    Over the past couple of weeks I must have followed the best part of a dozen so-called tutorials on how to get maps running in an app and not a single one has worked – they all seem to be missing something or other that I’ve been unable to work out.

    But your tutorial worked first time. Absolutely superb. Thank you again.

  6. herry on March 4, 2013 at 12:35 am

    i try your source and I am getting a “Binary XML file line #7: Error inflating class fragment” error.
    please help me

    • george on March 4, 2013 at 5:10 am

      Please ensure that, Step 7 (Link to Google Play Service Library) is done

      • herry on March 4, 2013 at 6:16 am

        i have did it (step 7) …. pfffttt

        • george on March 4, 2013 at 6:34 am

          Ensure that the class MainActivity is extending the class FragmentActivity

  7. beerie on March 4, 2013 at 6:38 am

    i can run this application.. but the map does not appear and i got message in log cat –> “could not find class ‘maps.i.k’, referenced from method maps.z.ag.a

    • george on March 4, 2013 at 7:20 am

      >>could not find class ‘maps.i.k’, referenced from method maps.z.ag.a

      Re-install the application and try again. Before re-installing, ensure that, you entered the correct api key.

      • beerie on March 4, 2013 at 7:43 pm

        i use your api… can’t it?

    • beerie on March 4, 2013 at 8:36 am

      i use 4.04 in my phone… does it matter?

  8. Shameera on March 8, 2013 at 12:16 am

    I tried this one but its not worked on Galaxy Y and always suffer with Force Close error. But its works nicely on some kind of devices like Xperia U , Xperia Sola, Asus nexsus 7 tab etc.. Can u tell me why its not worked on Galaxy y (2.3 gingerbread)…

  9. Nano on March 17, 2013 at 12:07 am

    hey.. could u please help me in building my google map v2 .. its driving me crazy i’ve been trying for a whole week and i can’t figure out whats wrong with the program ..
    here it is my code ..
    xml file:

    my java file:
    package com.example.e_order;

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import com.google.android.gms.maps.SupportMapFragment;

    public class Order_map extends SupportMapFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_order_map, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    }
    }

    my mainifist.xml :

    my logcat when executing the map activity:
    03-16 20:21:20.378: E/AndroidRuntime(745): FATAL EXCEPTION: main
    03-16 20:21:20.378: E/AndroidRuntime(745): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.e_order/com.example.e_order.Order_map}: java.lang.ClassCastException: com.example.e_order.Order_map
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.os.Handler.dispatchMessage(Handler.java:99)
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.os.Looper.loop(Looper.java:123)
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.app.ActivityThread.main(ActivityThread.java:4627)
    03-16 20:21:20.378: E/AndroidRuntime(745): at java.lang.reflect.Method.invokeNative(Native Method)
    03-16 20:21:20.378: E/AndroidRuntime(745): at java.lang.reflect.Method.invoke(Method.java:521)
    03-16 20:21:20.378: E/AndroidRuntime(745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    03-16 20:21:20.378: E/AndroidRuntime(745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    03-16 20:21:20.378: E/AndroidRuntime(745): at dalvik.system.NativeStart.main(Native Method)
    03-16 20:21:20.378: E/AndroidRuntime(745): Caused by: java.lang.ClassCastException: com.example.e_order.Order_map
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    03-16 20:21:20.378: E/AndroidRuntime(745): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    03-16 20:21:20.378: E/AndroidRuntime(745): … 11 more

    Thank you :)

  10. Basribaz on March 25, 2013 at 6:12 pm

    [2013-03-25 20:39:32 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/google/android/gms/auth/GoogleAuthException;
    [2013-03-25 20:39:32 - LocationDistanceTimeMapV2] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/google/android/gms/auth/GoogleAuthException;

    please help solution….

    • george on March 25, 2013 at 6:22 pm

      Please ensure that, you entered the correct API key in the file AndroidManifest.xml

  11. Ahsan Feroz on April 11, 2013 at 6:46 pm

    Hi George,

    It’s a great tutorial, in a simple way.

  12. andry yudha on May 6, 2013 at 2:25 pm

    hai george, i have been tried it, but white screen with zoom in and out always appears. the key is correct,.. map isn’t appear

  13. Deepthi on May 6, 2013 at 5:40 pm

    Hi… I have tried this way, but I am getting error that

    [2013-05-06 14:07:52 - KiwiApp] ERROR: Application requires API version 11. Device API version is 10 (Android 2.3.6).

    I am using android mobile of 2.3.6 version.I am designing my app in android 4.0.3 in Eclipse.
    please solve my problem.

    thanks in advance

  14. nischal on May 25, 2013 at 10:21 pm

    The sample code fails to execute in my android 2.3.5 Htc wildfire S
    05-25 22:17:55.151: E/AndroidRuntime(32651): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationgooglemapv2demo/in.wptrafficanalyzer.locationgooglemapv2demo.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

  15. Javed on May 30, 2013 at 5:57 pm

    I got force close in my Galaxy Y. please help me….

    • george on May 30, 2013 at 6:01 pm

      Hi Javed,
      Please check your logcat to get a clue on the error

  16. anitaa on June 15, 2013 at 9:37 am

    Hi…I am very new to android and I keep getting the following error every time I run the app and it force closes, not the device and on the emulator.

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{in.wptrafficanalyzer.locationgooglemapv2demo/in.wptrafficanalyzer.locationgooglemapv2demo.MainActivity}: java.lang.ClassNotFoundException: Didn’t find class “in.wptrafficanalyzer.locationgooglemapv2demo.MainActivity” on path: /data/app/in.wptrafficanalyzer.locationgooglemapv2demo-1.apk

    I am not sure what the problem is. Please help

  17. eon on June 24, 2013 at 4:26 am

    hi there,

    i tried to run your demo. used new Android studio and a galaxy s3.

    one issue i got is:
    getMenuInflater().inflate(R.menu.activity_main, menu);
    delivers a compiler error (not found…)

    i removed the complete method. so it compiles.

    when i run the app on my phone, it stops immediately after it starts with a white background.

    no map to see.

    please help.

    eon

  18. waseem ud din on July 3, 2013 at 12:34 pm

    Hi,
    George
    I am getting problem in my activity file
    when i add xmlns in fragment it will give error “unexpected xmlns”
    kindly help me out.
    thanks

    • akhwan on August 16, 2013 at 12:13 pm

      same trouble
      i solve this just by do project clean

      • John "JD" Donaldson on January 24, 2014 at 11:54 pm

        thanks, I had the same prob. and cleaning the proj. worked.

  19. waseem ud din on July 3, 2013 at 1:50 pm

    i followed your specified steps but i m getting error on emulator
    “unfortunately “app” has been stopped” how to resolve it.

    • george on July 3, 2013 at 1:55 pm

      Please try in a real Android device

  20. prasad on July 11, 2013 at 9:03 am

    Hi I didn’t the mapview in emulator nor in mobile. I followed your steps eventhough i didn’t get. Also I have tried by importing your source code,but couldn’t get rid off. Please suggest me how to overcome this. Thank you.

  21. nasrin on October 5, 2013 at 12:44 pm

    hi
    I do same as you but i have a problem : when i run my app it say that this app won’t run unless you update google play services
    what can i do

  22. anooz on October 28, 2013 at 5:56 pm

    i tried it on a real phone GT-S7562. But it doesn’t loads the map. it gives the + – buttons but doesn’t load the app…please reply

    • anooz on October 28, 2013 at 5:58 pm

      did it on API level 10. should i download level 16…

  23. praveen on November 22, 2013 at 8:52 pm

    java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationgooglemapv2demo/in.wptrafficanalyzer.locationgooglemapv2demo.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

    This error and authication faliure i tried whole week can u please help me

  24. Swetha on December 5, 2013 at 9:56 pm

    Hi,

    Im getting this error while running the app. Please help me out in this. Its urgent..

    java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationgooglemapv2demo/in.wptrafficanalyzer.locationgooglemapv2demo.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

    Thanks…

  25. sajid khan on December 16, 2013 at 3:44 am

    Hi!
    I’ve got the error plz help me I’ve tried different ways to fix but fail.
    java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationgooglemapv2demo/in.wptrafficanalyzer.locationgooglemapv2demo.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment

    plz help me out.

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner