Friday, December 21, 2012

A simple example using Google Maps Android API v2









To implement Google Maps Android API v2 , follow the steps:
[Updated@2015-10-13: Another series base on Android Studio is on-going:
Step-by-step of Android Google Maps Activity using Google Maps Android API v2, on Android Studio]

Now, it's time to implement your code to add Google Maps Android API v2. Notice that you have to keep both your project and google-play-services_lib opened in Eclipse.


Please note that the code below is only useful for testing your settings in an application targeting Android API 12 or later, This code should not be used in a production application.

Modify layout to add frqgment of com.google.android.gms.maps.MapFragment.
<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>


Main code, actually nothing changed.
package com.example.androidmapsv2;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

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

}


A simple example using Google Maps Android API v2


download filesDownload the files.

Exported AndroidMapsV2.apk



Related:
- When Android Meets Maps, Google I/O 2013 video


47 comments:

  1. please give me some sugestion on google map v2 fro android

    ReplyDelete
  2. im sorry to have bothered you with suck silly questions, seems that you cannot test the maps on emulators so i had to buy a cable and test it on my phone(android 2.3.3 hence the SupportMapFragment) and now it works.

    ReplyDelete
  3. Hello cioro,

    You mentioned fail with ClassNotFOundException is run on Android Emulator?

    I just tried to run on Emulator, I got another error of "Google Play Store is missing", not ClassNotFOundException. (refer Google Play services is not supported on the Android emulator)

    ReplyDelete
  4. It is really useful thank you so much! How about step by step how to load a map or offline city tour map for tourist like or commuter. thanks in advance, more power!

    ReplyDelete
  5. OmG...
    I have a project in mind which requeries too many more elements than this simple one; But I still see yours as very complex :(
    Anyway, good job and great blog!
    I´ll keep reading you

    ReplyDelete
  6. I think this comment below must be inserted in this post, specially at the top of both XML and Code:

    "Please note that the code below is only useful for testing your settings in an application targeting Android API 12 or later, This code should not be used in a production application. ""

    This comment is written in the Android Development Guide. Many developers who have experienced the old Android version before are likely to miss this important comment, I think.

    ReplyDelete
  7. Hello BBonDoo,

    Thanks for your comments. I added comment in the post.

    ReplyDelete
  8. Anonymous Examples on GoogleMaps v2.i spend two days for learning these concepts,but i cant get clarity explanation on these concepts.After Two days i find your blog.Its very usefull for me.
    Thank you so much friend.

    ReplyDelete
  9. I downloaded your APK and installed in my Android 2.3.3 device. The moment I start this application, it crashes. Is there anything specific I need to do prior to running this app??

    ReplyDelete
  10. hello varun,

    Yes, this version use MapFragment, target API Level (3.1.x) or above. To target for older API level, please refer Using SupportMapFragment.

    And, Google Maps Android API v2 Require OpenGL ES version 2.

    ReplyDelete

  11. When i am using Mapsv2 in Fragments it not supported,because their is no getSupportedMapFragment method.i need to implemented these concept(MapsV2)in TabActivity using Fragments.please post any example like that way.

    ReplyDelete
  12. Hi....

    I have seen your example code. I implement your code and run the app in my emulator. at that time i get following error message.
    Errors are: 1) "java.io.IOException: Server returned: 3", 2) "at android_maps_conflict_avoidance.com.google.googlenav.map.BaseTileRequest.readResponseData(BaseTileRequest.java:115)", 3) "at android_maps_conflict_avoidance.com.google.googlenav.map.MapService$MapTileRequest.readResponseData(MapService.java:1473)", 4) "at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.processDataRequest(DataRequestDispatcher.java:1117)", 5) "at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.serviceRequests(DataRequestDispatcher.java:994)", 6) "at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher$DispatcherServer.run(DataRequestDispatcher.java:1702)".

    And only grid is display in emulator.

    Please give me solution of it as soon as early.
    Email is: sneha2011.jobs@gmail.com

    ReplyDelete
  13. hi,


    I have run this exmaple but i find many errors,
    Please fixit it and notify me.
    03-11 13:09:03.027: E/AndroidRuntime(3077): FATAL EXCEPTION: main
    03-11 13:09:03.027: E/AndroidRuntime(3077): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
    03-11 13:09:03.027: E/AndroidRuntime(3077): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.Activity.onCreateView(Activity.java:4663)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.Activity.setContentView(Activity.java:1867)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at com.example.androidmapsv2.MainActivity.onCreate(MainActivity.java:11)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.Activity.performCreate(Activity.java:5008)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.ActivityThread.access$600(ActivityThread.java:130)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.os.Handler.dispatchMessage(Handler.java:99)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.os.Looper.loop(Looper.java:137)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at android.app.ActivityThread.main(ActivityThread.java:4745)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at java.lang.reflect.Method.invokeNative(Native Method)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at java.lang.reflect.Method.invoke(Method.java:511)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    03-11 13:09:03.027: E/AndroidRuntime(3077): at dalvik.system.NativeStart.main(Native Method)


    Regards

    ReplyDelete
  14. hello mohammed Bassam Saleem,

    I'm not sure your case.

    How you import the Support Library? Please read Tips to add Support Library.

    Please reply if it can fix it or not.

    Thx

    ReplyDelete
  15. please help me in my problem ...
    i've been trying to run the new google maps v2 but every time i execute the program crashes .. and i cant figure out what is wrong in the code.. could anyone help me please..
    here is my code :
    my java :
    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 xml:



    my mainfist.xml:



























































    my logcat when running the map :
    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 :)

    ReplyDelete
  16. This worked for me. Thank you, very, very much.

    ReplyDelete
  17. How can you fix a image (my personal map) to specific coordinates and work above this??

    ReplyDelete
  18. hii eric can you give me tutorial Location Based Service using google map V2, this is for my college assignment, please help me :(
    if you can please send me mail at m.agessy.barazi@gmail.com

    ReplyDelete
  19. hi

    can you please tell me how to invalidate map. if i give some position it shows correctly at first time but at second time it shows previous latitude, longitude values on map..

    ReplyDelete
  20. hello ankit sharma,

    Can you explain in more details?

    Are you looking for animateCamera.

    ReplyDelete
  21. Very very useful!! a lot of thanks.

    ReplyDelete
  22. Hi,

    Have been following this post, after trying to set up Maps may a times myself. Still very stuck here.

    EVerything has been added, googleplayservices.jar, the compatibiltiy.jar as suggested in the tips.

    My problem is I keep on getting:
    "Cannot resolve symbol MapFragment" and it is underlined red and my application wont work.
    If I change it to "SupportMapFragment (and the necessary changes in the Main file etc.. same problem)

    What do you suggest?

    ReplyDelete
  23. thank you very much!!! your article is the best!! You blog is first of the last 59 which i looked and it is WORKS!!!!

    ReplyDelete
  24. Hello i an fresher in android , how to use apiV2 map without using fragment activity?

    ReplyDelete
  25. I found this article is simple and easily understandable but, I'm getting a log err as below while trying to run it on api level 17.

    I'm calling
    int status = GooglePlayServicesUtil
    .isGooglePlayServicesAvailable(getApplicationContext());
    if (status != ConnectionResult.SUCCESS) {
    Log.e("GP", "SC");
    } else if (status != ConnectionResult.INVALID_ACCOUNT) {
    Log.e("GP", "IA");
    } else if (status != ConnectionResult.SERVICE_MISSING) {
    Log.e("GP", "SM");
    } else if (status != ConnectionResult.SERVICE_INVALID) {
    Log.e("GP", "SI");
    } else if (status != ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) {
    Log.e("GP", "SUR");
    } else if (status != ConnectionResult.SERVICE_DISABLED) {
    Log.e("GP", "SD");

    } else {
    Log.e("GP", "value is=" + status);
    }

    it gives output status as, INVALID ACCOUNT

    Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above

    Application installed and launched it shows zooming controls but blank map.

    No other errors found in Log or console.


    ReplyDelete
  26. hello Globedat,

    are you use your own APK key? (Debug key/Release Key)

    For GooglePlayServicesUtil.isGooglePlayServicesAvailable(), please read here.

    ReplyDelete
  27. Hai, I tried google map program.it runs successfully.But not viewalbe on emulator... Please me some suggestion

    ReplyDelete
  28. Hello Thala Arun,

    I don't suggest to run Maps Apps on Emulator!

    ReplyDelete
  29. how to display two google map v2 in tabhost ...

    ReplyDelete
  30. I got this error when running even the simplest googlemap v2 code that previously run OK.."Error inflating class fragment" I'm not quite sure what's going on. All my google map apps suddenly facing this problem able to compile but crash when execute on my galaxy note 3.

    ReplyDelete
  31. Hi,Thanks for the awesome tutorial.
    I followed the steps but, only white screen is visible when i open the app.Pls help me to fix my problem.

    ReplyDelete
  32. how to show polyline from mysql database to android google maps?

    ReplyDelete
  33. hi Eric,
    I'm trying to use your codes to implement google play in my projrct.But there is a problem occuring in the first line of manifest file.that tells "The processing instruction target matching "[xX][mM][lL]" is not allowed".what i'ii do??

    ReplyDelete
  34. Most probably "The processing instruction target matching "[xX][mM][lL]" is not allowed" is caused by there some visible or invisible character exists before the <?xml ?> declaration.

    Please read: Here

    ReplyDelete
  35. Hello i am stuck in a problem i am making a app linked that displays webpages on start at defined url
    but i am stuck on a argument that i got suceeded in making it for api 11
    but not in api 17 or above mainly if u can help me by emailing me at this email address it would help a
    lot

    ReplyDelete
  36. hello vasu soni,

    Have you add permission of "android.permission.INTERNET" in Manifest file?

    ReplyDelete
  37. This comment has been removed by the author.

    ReplyDelete
  38. Can I have the whole project file?

    ReplyDelete
  39. Sir can my app doesnt display the map it only shows the icon google on the bottom please help!

    ReplyDelete
  40. hi
    Why the Gps is vary from device to device if the code working on moto series but the same code is not working on other phone and the same way the code is working on the lowest version of android..i could on the gps and the location services and then also it can`t be give the result

    ReplyDelete
  41. Hi,

    If I have multiple users which I want to display the location of? How would I do that? Thanks.

    ReplyDelete
  42. HI

    Thanks for the code
    i want to animate the drag and drop, is it possible?

    ReplyDelete