Friday, December 21, 2012

A simple example using Google Maps Android API v2









To implement Google Maps Android API v2 , follow the steps:

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


34 comments:

Anonymous said...

please give me some sugestion on google map v2 fro android

cioro said...

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.

Andr.oid Eric said...

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)

Asiano Pinoy said...

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!

diseño web said...

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

BBonDoo said...

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.

Andr.oid Eric said...

Hello BBonDoo,

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

MCA Boys said...

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.

varun said...

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

Andr.oid Eric said...

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.

MCA Boys said...


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.

Anonymous said...

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

Andr.oid Eric said...

please notice: Google Play services is not supported on the Android emulator.

mohammed Bassam Saleem said...

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

Andr.oid Eric said...

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

nano said...

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 :)

Katerina Petkova said...

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

Pedro Brochero said...

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

Muhammad agessy barazi said...

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

ankit sharma said...

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

Andr.oid Eric said...

hello ankit sharma,

Can you explain in more details?

Are you looking for animateCamera.

Alejandro Velasco García said...

Very very useful!! a lot of thanks.

P Joshi said...

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?

Серёжа Шевченко said...

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

Anonymous said...

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

Globedat said...

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.


Andr.oid Eric said...

hello Globedat,

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

For GooglePlayServicesUtil.isGooglePlayServicesAvailable(), please read here.

Thala Arun said...

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

Andr.oid Eric said...

Hello Thala Arun,

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

Karthick V said...

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

SLum said...

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.

Fadil P said...

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.

PREM said...

thanks a lot yaar.....

Mohamed Ibrahim said...

Thanks. This is very helpful.