To implement Google Maps Android API v2 , follow the steps:
- Setup Google Play services SDK in Eclipse.
- Displaying the SHA1 certificate fingerprint, for debug and release.
- Generate Debug API Key for Google Maps Android API v2 service, for testing and coding.
- Add reference library google-play-services.jar to project using MapFragment and SupportMapFragment.
- If you have NoClassDefFoundError when using Support Library, check Tips to add Support Library.
- (updated@2013-07-26: now support...) Don't test App using Google Maps Android API v2 on Android emulator, it's not supported.
- Modify AndroidManifest.xml for Google Maps Android API v2.
- Coding, described in this post.
- Create and Obtain API Key for Google Maps Android API v2 service, replace Debug API Key with Release API Key for API.
- May be you have to Turn off Lint Error Checking when Export Signed Application Package.
- Using SupportMapFragment for device run API Level below 12.
- Google Maps Android API v2 with blank map displayed, wrong API Key assigned.
- Include open source software license information/Legal Notices in your app using Google Maps Android API v2.
- Check if correct Google Play Service available for Google Maps Android API v2.
- Get GoogleMap from MapFragment/SupportMapFragment
- Display my location on Google Maps Android API v2
- Set map type of GoogleMap
- Detect user touch on GoogleMap and animate to the touched location
- Using MapFragment instead of SupportMapFragment
- Detect long click on map and add marker
- Detect MarkerClick and add Polyline
- Draw Polygon on GoogleMap
- Implement Draggable Marker
- Display ZoomControls, Compass and MyLocation buttons on GoogleMap
- Enable/Disable various gestures for GoogleMap
- Turn on Traffic Layer
- Change color of default marker
- Implement bouncing marker
- Create custom info contents
- Implement Custom InfoWindowAdapter with dynamic icon
- Detect Info Window Click, implements OnInfoWindowClickListener
- Display another layout over GoogleMap
- Implement LocationSource and LocationListener
- Detect and animate to user location
- Create MapFragment and GoogleMap using Java code
- Set options in XML
- Move Google Maps V2 with auto best zoom
- Animation on Google Maps Android API v2
- GoogleMap animation with zoom
- Google Maps V2 Animation with bearing
- Convert between LatLng and Location
- Draw Circle on GoogleMap, with Google Play services v3.0
- Draw tranparent circle
- Implement OnMyLocationChangeListener
- Draw hollow Polygon on Map, using addHole()
- Video of Kick-start Google Maps Android API v2 Development, by GoogleDevelopers
- Implement dual MapFragment
- Retain instance of MapFragment
- Draw Polyline from touched location to MyLocation
- Unexpected namespace prefix "xmlns" found for tag fragment on xmlns:map="http://schemas.android.com/apk/res-auto"
- To test apps using Google Play Service on emulator, install AVD with Google APIs platform based on Android 4.2.2 or higher
- Introduction Video - New Features in the Google Maps Mobile APIs for Android and iOS
- Very nice intro at updated Google Maps Android API v2 documentation page
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);
}
}
Download the files.
Exported AndroidMapsV2.apk
Related:
- When Android Meets Maps, Google I/O 2013 video
47 comments:
please give me some sugestion on google map v2 fro android
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.
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)
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!
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
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.
Hello BBonDoo,
Thanks for your comments. I added comment in the post.
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.
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??
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.
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.
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
please notice: Google Play services is not supported on the Android emulator.
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
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
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 :)
This worked for me. Thank you, very, very much.
How can you fix a image (my personal map) to specific coordinates and work above this??
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
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..
hello ankit sharma,
Can you explain in more details?
Are you looking for animateCamera.
Very very useful!! a lot of thanks.
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?
thank you very much!!! your article is the best!! You blog is first of the last 59 which i looked and it is WORKS!!!!
Hello i an fresher in android , how to use apiV2 map without using fragment activity?
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.
hello Globedat,
are you use your own APK key? (Debug key/Release Key)
For GooglePlayServicesUtil.isGooglePlayServicesAvailable(), please read here.
Hai, I tried google map program.it runs successfully.But not viewalbe on emulator... Please me some suggestion
Hello Thala Arun,
I don't suggest to run Maps Apps on Emulator!
how to display two google map v2 in tabhost ...
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.
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.
thanks a lot yaar.....
Thanks. This is very helpful.
how to show polyline from mysql database to android google maps?
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??
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
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
hello vasu soni,
Have you add permission of "android.permission.INTERNET" in Manifest file?
Obviously yes i did
Can I have the whole project file?
Sir can my app doesnt display the map it only shows the icon google on the bottom please help!
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
Hi,
If I have multiple users which I want to display the location of? How would I do that? Thanks.
HI
Thanks for the code
i want to animate the drag and drop, is it possible?
hello v v,
Is it? Implement Draggable Marker.
Post a Comment