Android Google MapView Tutorial — Done Right!
May 6, 2010 109 Comments
For a total newbie to the world of Android it would take a while to get this tutorial working. There are too many quirks, the example on the Documentation site did not work properly….so I’m publishing this in case someone comes across the need for a working version of it. Keep in mind that almost all of this code is from Google. Its just purified, corrected and presented in a more understandable manner.
NOTE: I’m a MS Windows based developer, hence the “windows” references (and lack of Mac/Linux stuff in here). However, the code should run no matter where you are developing.
STEP 1) GET THE DEVELOPMENT ENVIRONMENT SET UP
Easy way to get started is to get the Eclipse Java EE IDE for Web Developers, install the Android SDK, get the latest versions of all the frameworks, the Google Maps Add-On, etc and then start learning.
Here’s where you can do that in Eclipse after installing the SDK:
“Window>Android SDK and AVD Manager” on the Menu.
Then you can install the available stuff that you need.
STEP 2) GET THE GOOGLE MAPS API KEY
To allow you to show maps on your application, Google Maps API needs to identify you and your application (even if you are simply developing). For this you need the API Key for the Android platform.
You can get this by creating an md5 checksum of the debug certificate for you map application (in this case the tutorial).
Find your debug.keystore at
* Windows Vista: C:\Users\<user>\.android\debug.keystore
* Windows XP: C:\Documents and Settings\<user>\.android\debug.keystore
Then use the Keytool (found at C:\Program Files\Java\jdk1.6.0_20\bin) and get the md5 checksum by executing this:
keytool -list -alias androiddebugkey -keystore “C:\Documents and Settings\<user>\.android\debug.keystore” -storepass android -keypass android
Don’t forget the quotes around the path when in Windows!
That should get you the md5 checksum that you can plug into this site and get your Google Maps API Key for Android!
STEP 3) CODE IN!
Start a new “Android Project”
Make sure you select the relevant version of Google APIs (in case there’s a newer version by the time you are working on this).
Add a new class called “HelloItemizedOverlay”. You can do that by going to HelloGoogleeMaps2>src>com.example.HelloGoogleMaps2, doing a right click and choosing New>Class.
Follow the above image to choose your options. Then click Finish.
Your project’s Package Explorer should look something like this on Eclipse (my project was called HelloGoogleMaps, yours should be called HelloGoogleMaps2):
Then put these contents into the appropriate files:
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, HelloGoogleMaps!</string> <string name="app_name">Hello,GoogleMaps</string> <string name="mapskey">YOUR API KEY</string> </resources>
main.xml
<?xml version="1.0" encoding="utf-8"?> <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="YOUR API KEY" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.HelloGoogleMaps2" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloGoogleMaps2" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="com.google.android.maps" /> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
HelloGoogleMaps2.java
package com.example.HelloGoogleMaps2;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class HelloGoogleMaps extends MapActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
GeoPoint point = new GeoPoint(30443769,-91158458);
OverlayItem overlayitem = new OverlayItem(point, "Laissez les bon temps rouler!", "I'm in Louisiana!");
GeoPoint point2 = new GeoPoint(17385812,78480667);
OverlayItem overlayitem2 = new OverlayItem(point2, "Namashkaar!", "I'm in Hyderabad, India!");
itemizedoverlay.addOverlay(overlayitem);
itemizedoverlay.addOverlay(overlayitem2);
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed()
{
return false;
}
}
HelloItemizedOverlay.java
package com.example.HelloGoogleMaps2;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context)
{
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay)
{
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i)
{
return mOverlays.get(i);
}
@Override
public int size()
{
return mOverlays.size();
}
@Override
protected boolean onTap(int index)
{
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
STEP 4) RUN!!!
If you do not already have the AVD (Android Virtual Device) for the Google Maps Application, create it by going to “Window>Android SDK and AVD Manager” and selecting Virtual Devices on the left hand side.
Now that all the code is in place, you can run the application:
Right click on the project and choose Run As Android Application
This is what you should see (provided all the code was entered error free and the API Keys were entered properly):
On Zooming In and clicking on the Placemarkers, we can see the following!
This should be the end result. If you have any questions regarding the code, let me know!








Thanks for ur information
This is the best google map tutorial I have found! Well done. Hope you have time for more! Thanks very much.
Thank You Boss, you have just saved me from the death, I was really frustrated from the original tutorial. thank you!
Agreed. Still don’t know what went wrong with the original code though, but atleast I got it working now. Thanks!
Thanks Dude. Been trying for days this is the only tutorial that worked.
wow it’s strange that with Google Maps API being one of the advantages of Android over iPhone that they would screw this up. You cleared it up very well. Thanks.
I think Google Maps API is pretty robust. The example that was provided (at least at the time I was working on it) was not rightly done and was frustrating to me as a beginner. That’s the reason I had put this post out.
wow.. great job…… ur tutorial helped me a lot….. thankyou..
Thanks, couldn’t get the google tutorial to work
Thanks so much!
Cleared that last little piece missing from the tut on the dev site.
Thanks so much, your simplified example saved me from chewing for too long on that Google tutorial!
Thanks for this. I got it working with the points, however the map of the world isn’t showing up (though the points show up where they should be and are working). Any ideas?
Not sure exactly what might be causing that in your code, but let me take a shot: Make sure to check your main.xml is done right. Make sure to put YOUR Google maps api key in there. If that does not fix your problem, then take a look at the AndroidManifest.xml and make sure that the permissions and proper libraries are added in.
Hope this helps!
This is a maps api key issue. If you don’t supply a valid key, you don’t get access to the map tiles.
Thanks for such a fast reply. I checked both and they’re consistent with your working copy. I’m using the Google API 2.1-update1 API Level 7 version avd. Not sure if that affects it at all. I’ve tried it on there and using Level 8. Definitely a strange issue, as it knows the location of the objects, only the google map isn’t coming through.
That certainly is odd. This was dev on 2.1 v8. The map tiles aren’t showing up for your app. That makes me think that either the map key/keystore is messed up. Else, make sure there are no typo-s in the xml files (easy to make, tough to spot!). Also make sure you are giving it time to load…tiles take some sometimes in virtual environment.
Cheers!
Hey Thank you for your tutorial , its certainly much more straight forward than the one shown in the actual site. However I do have a problem running both tutorials in which I end up with the same problem where the console says ” Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY . Have you encountered this problem before ? Thanks alot for your tutorial anyway !
Ok , I just found out the root where this issue might have arose from.
Took me a while to figure it out … Bummer
This was because I using the AVD of the Target Name – Android-2.1-update1 .
Since the HelloGoogleMap project was created from a Build Target setting – GoogleAPIs – (Platform)2.1 -update1 , I should use the AVD with the same Target Name as well. And it did work
Then I usually encounter a problem in the AVD where it says some device is not responding or busy whenever i run an application. Is it normal for AVD to display that ? Isn’t it a virtual machine ? Why is it that it will lag ?
Glad u figured that out. I could not replicate it on my m/c. Not sure why there’s a lag, but from what I’ve seen, usually VDs are slower to boot up every time we’re debugging. Also depends on the hardware u’re running it on. The first few times, it took me abt. 4-5 minutes on my laptop, but was quick after that (if no changes were made)
Do you know of any good Android examples of how to display or get your current GPS location.
I can’t seem to find a go example that works.
Thanks
: )
Small mistake:
it says in the code
public class HelloGoogleMaps extends MapActivity
but it should be
public class HelloGoogleMaps2 extends MapActivity
may i ask a question?
i have once generate MD5 checksum long time ago but i forget where i keep it.
i generate it once again as you suggest above, and get this error: keytool error: java.lang.runtimeexception: usage error, and is not a legal command.
is this checksum may be generated only once? or what did i do wrong? please help
Hello, I really like this Tutorial. It is very helpful. But I wanted to ask if you maybe have an example how I can get the Geopoint from a tap. Or also place an overlay item at this position.
So you should just see a plain map and if the user taps or dubble taps it should show a marker. I am searching for days now for an example but every example which seems to be promising just shows how to identify the list item with a tap. I hope you can help me or at least give me a link to an tutorial
Hai,
This is Rgav , i follow ur tutorial it’s very useful me , ang getting output ….also
thanks
Hi,
The tutorial is really good and I got everything to work as is. However if I add in a point of my own it refuses to read it in and displays only the 2 outlined here. Any thoughts as to why this happens?
Thanks
Ryan
Sorry I fixed the issue just after I posted, it was just a stupid mistake.
Thanks for the tutorial
Thanks for the great details on this. I noticed that once the onTap text is displayed, I cannot get back to the map and tap another point. Any idea what might be the problem or how to do this?
Thanks for any help
Thanks! Spent hours trying to figure out why I couldn’t get the google tutorial to work. You have provided a nice, clean example that actually worked after following your steps. All I need to do no is go back through and find out why none of the others didn’t and what makes this tick.
Kudos!
Thanks a lot for your tutorial !!! Merci bcp!
Thank you for your outstanding tutorial.
I followed all the steps but do not see the maps. The android icon and the overlay items are displayed.
I have checked the API key and seems to be correct.
Can you suggest what else could be the issue?
Thank you.
Thanks for the compliment Parag. The post came about because I was frustrated that Google’s tutorial was incorrectly done.
From what you said in your post, if map tiles are not visible, it could be one of two things
1) Check your maps key
2) make sure your app has the proper settings to access the internet.
I appreciate your prompt reply.
How do I check that the map key is correct ?
I can access web sites so I think it is connected to internet. I do see small squares with the android icon on the top. Does loading of map take a long time ? i have waited for more than 5 minutes.
Thanks.
Parag,
I was having the same problem and for me it was the key. I found a great keytool plug-in for Eclipse the let you view your M5 fingerprint to use to get your key. Just search Eclipse keytool plug-in. Hope this helps.
This is the error seen in the error log
“No command output when running: ‘am start -n com.example.HelloGoogleMaps2/com.example.HelloGoogleMaps2.HelloGoogleMaps2 -a android.intent.action.MAIN -c android.intent.category.LAUNCHER’ on device emulator-5554″
What does it mean ? Could this be the reason for maps not being displayed ?
Thanks.
I’m not sure what the exact problem might be in your case. It could be an issue in the AndroidManifest file. I’ve seen the error when the main activity is missing. If nothing else works, I suggest you create the project from scratch. Make sure Eclipse is configured properly and the right Android version’s Virtual Device is targeted in your project. I’ve seen people having these sorts of issues when they mix up the AVD version.
Thank you for your suggestions.
Thank You!!!
Code Magician,
Like I said in my other post thanks for the tutorial.
I had no problems getting it to work when it was the only activity, but when I tried to add other activities ( A home screen with a button linking to it) it would not work. If I set it back with the “main” and “launcher” intents it works.
Any ideas what going wrong? Thought I’d post it kind of general just in case. If you need code and errors I can post them also.
Thanks so much!
-Nick
Yea! I got it to work. Some how I moved the other activitys up into the first activity. If that makes scense.
You should do some more tutoruals, like a list view with headers
The ones I’ve found were hard to follow compared to yours.
One last, Thank You.
-Nick
Thanks for posting the solution to the issue you were having! Sorry, didn’t get to your question in time. I will be posting more articles on android and webOS…just have to eke the time out of the week to get it done
How can you now draw a line between those 2 markers????
Thanks
Pingback: A Map with the Wrong Direction « reinermatthew
Hey Matthew, please check your eclipse installation. Make sure u have the latest version of JRE/JDK installed as well.
hi, I cant stop myself without thanking u.Really u relieved me after 3 weeks of frustration .
this is the best tut for gmap.
Thank u so much.
hi, can u please send me some more tute on gps and googlemap or some e-book if u have.
thanks.
hello experts!!!
i am facing same problem map don displayed in emulator though all the setting for the avd is rite and all the file in the project is rite. no error displayed in the consle
only white with small square are seen
Thank you for copy pasting the tutorial from following android developer site, without any explanation and guidelines.
http://developer.android.com/resources/tutorials/views/hello-mapview.html
If you read the post, you will see that I did say this a copy….a WORKING copy….BIG difference. If the original site was changed so that the code works..then awesome….this just a copy.
Cheers mate!
Brilliant tutorial, absolutely vital in helping me to get the hellogooglemaps application to work properly! – Google Android tutorial writers please take note!
Finally! Great piece – thanks!
Excellent tutorial! Really got me ‘over the hurdle’ and on my ay. I do have one request. Has anyone been able to add a label to the individual markers?
hello sir!
I tried this code but I m getting only a shadow grid in the background and i couldnt get the map pls help me I m in the mid of my project Pls Pls
If you are only getting the grid, you need to go back and check the registration of your API key with google: you need to have a valid api key declared for your mapview object inorder for map graphics to load.
Just wanted to give you a big thanks — spend hours trying to figure out what was wrong with the documentation’s tutorial. Thanks again for taking the time to put this up — it really helped!
thank you so much for putting this together. this helped tremendously as i spent a lot of time on the original android tutorial which was less than helpful!
Hello,
Question – if I need to show different items then all need to have a different Overlay because of default drawable in constructor?
best regards ,
ronnixw
Yeah, I think it is better to have separate layers for showing different types of objects.
Thank you so much for making this. The Google tutorial left out a few important steps to make this work. If you could add a little more info on what each command is doing and why, that would make this tutorial a little better. But, it was great and helped me with the map view.
Good job man. Thanks.
Hi,
At step 2) i run the keytool.exe it opens but the window closes after a few secunds and i can’t run the comands…
Any idea what cloud be wrong?
Regards
Mike
It is a command line tool. Are you double clicking on it or running it from your command line? You aren’t supposed to open the tool…open up cmd.exe (assuming Windows) and then follow instructions.
i’m using win7 so i have to do it in cmd.exe?
Thanks
Yes…and make sure that when you find cmd.exe, right click on it and choose “Run as administrator” option.
I done what you said but when i insert the command it returs
“Illegal option: and
keytool -list [OPTION]..”
i have that line: “C:\Programas\Java\jdk1.7.0\bin>keytool -list -alias androiddebugkey -keystore “C:\Documents and Settings\Mike\.android\debug.keystore” -storepass android -keypass android”
What am i doing wrohg?
Thx very much.
Tugrul
how to fix the panning area to the overlayed image in mapview in android
Brilliant. Thank you very much.
Good work!!! i am having some problem running in android device ..map is not loading only icons are showing with “namaste…”.My device is lg P500 ..i have done exactly the same way as described by you jus changed the google api version to 2.2.
i am having some problem running in android device ..map is not loading only icons are showing with “namaste…”.My device is lg P500 ..i have done exactly the same way as described by you jus changed the google api version to 2.2.
I think Erik was having the same problem as you…Look at the comments section and see Justin’s reply. I recommend trying that. Tiles not coming up usually is user error, or internet connection not being set up right.
Hi I’m working on a map based app. The problem I’m facing is the maps take more time in loading.. any suggestions how to improve the map loading.
Not entirely sure…but maybe you could try lazy-loading the tiles surrounding the current viewing area?
not running send me the complete project at my id-
ravikumar@ethicstech.in,ravisaini1990@gmail.com
please send me as soon as possible
Thanks for the tutorial – also @developmentality offers this if you want to use different icons:
Drawable icon = this.getResources().getDrawable(R.drawable.someicon);
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
anotheroverlayitem.setMarker(icon);
You have to manually set the bounds of the icon before you can use it in setMarker().
If you don’t do this, none of your icons will display.
how to get the current location exactly on starting of a program with an image showing to that location????????please tell me??
yeah, even I want to know that
hiii howdoesitmatter
when you try it on real device and you used the function onlocationchanged() so when your location change it will shows you your current location.
Using this code i am not getting google map
thanks..good tutorial
Hi,
check your error codes. If you’re using a newer version of Eclipse you have to change the worrd icon to ic_launcher. Also make sure you put in your actual map key in main.xml
Job Well Done.
Much clearer. I was able to run with 1 exception, there is no “icon” but rather “ic_launcher”.
However this is probably due to changes in Eclipse versions or the Android Add on for Eclipse (I am using Eclipse version:
Eclipse IDE for Java Developers
Version: Indigo Service Release 1
Build id: 20110916-0149
(c) Copyright Eclipse contributors and others 2000, 2011. All rights reserved.
Visit http://eclipse.org/
The “New project” startup screens are also slightly different in this Eclipse version, but the requested info is still the same.
Also a big problem I had was a corrupt AVD. I hade all the correct code but got the most obsure errors you’ve ever seen (a literal WTF error was thrown in LogCat). If you’re seeng an odd emulator display or it’s taking forever to load, delete and remake your AVD. Wasted 1/2 day because of this problem.
Wow….it work fine..thank you for your tutorial…:))
I cant see icons on google map.. I drag an image to res-> drawable-hdpi drawable-ldpi and drawable-mdpi
Great tutorial! Puts the crappy Android Tutorials to shame!
how to put multiple icons on map with looping so we don’t have to call the object every time we put new icon?
ex :
GeoPoint point = new GeoPoint(30443769,-91158458);
OverlayItem overlayitem = new OverlayItem(point, “Laissez les bon temps rouler!”, “I’m in Louisiana!”);
GeoPoint point2 = new GeoPoint(17385812,78480667);
OverlayItem overlayitem2 = new OverlayItem(point2, “Namashkaar!”, “I’m in Hyderabad, India!”);
itemizedoverlay.addOverlay(overlayitem); —-> you have to state this as many icon we have
itemizedoverlay.addOverlay(overlayitem2); —-> how to do these with looping?
mapOverlays.add(itemizedoverlay);
Thank You Markk k k k k
ขอบพระคุณครับ ช่วยผมไว้ได้มากจริง ๆ ฮะ ><"
use the Keytool (found at C:\Program Files\Java\jdk1.6.0_20\bin) .. this worked for me!!! Great article !!!thnx!!!
Great tutorial, just could it be done with a custom View instead of just a drawable?
I would like to display a “popup” on the map, which would have a background picture (always the same), and a custom square picture over it, and some text.
For only 2 pictures I could dynamically create a Layer drawable; however I need a Text view too, so I would like a custom View to display on the map (I have an XML for that view with elements ImageView (id: overlay_picture) and TextView (id: overlay_text), with root id “overlay_layout” and it has a background). So how would I accomplish that?
I saw NOTHING about views neither in OverlayItem, nor in ItemizedOverlay, nor in Overlay.
Does anyone have an idea?
Oh I found out. If anyone else is interested, you can check here http://deckjockey.blogspot.com/2010/01/android-baloon-display-on-map.html
it mentions the usage of MapView.addView(…) which should solve my problem.
Hope it will help!
Hi,
This is great, but I’m trying to do something slightly different.
1) How would you give each point a different icon? And,
2) the popup with a title and description is perfect. But can you add a dismiss button and an action button to that popup?
Thanks!
guys im working on android project looking for any code to capture an image using the camera and uploading it to the database …
if any of you guys have it please e mail it to me as a folder to limoraka@gmail.com
Could you extend this tutorial to show a custom dialog on the overlay that uses a LayoutInflater to get an XML that has linkable text? Please and thank you.
Please reply to the following post:———————-It is urgent;————-
http://learnglobally.wordpress.com/2012/05/20/start-new-activity-in-android-clicking-on-a-marker/
ya.. thanks for your tutorial.this is very helpful..you would explain entire project in simple ways.
thanks…Rocking tutorial…
Thanks Code Magician – this tutorial also saved me from another long night.
To those who still only sees a grey grid you might need to force keytool to return an MD5 checksum (and not SHA1) by using the -v option:
keytool -v -list -alias androiddebugkey -keystore “C:\Users\\.android\debug.keystore” -storepass android -keypass android
Allan K. J.
Thank you Very much… Its very useful to me. Great tutorial. Thanks again.
that is a very nice tutorial thank you..!!
for me the map is not getting loaded because I am connected through proxy…
I have defined the proxy settings on the emulator and able to access the internet through browser…
but My application is not connecting through proxy…
It says “could not connect to the Connection factory client”
Could you please help me with this problem
Thanks very much sir, as a few have already mentioned this is the only tutorial that worked for me.
I did EXACTLY according to your instructions. but i am gettin “forced close” when i debug my program, it shows me following errors:
1)WARNING: Application does not specify an API level requirement!
2)Device API version is 10 (Android 2.3.4)
3)Launch error: Failed to connect to remote VM. Connection timed out.
i have made my app by selecting “Google API” platform “2.3.3″ in API “10″ and
it is showing the status “installed” for the following names
1) SDK platform
2) Samples for SDK
3) Google APIs
for the rest it is showing “uninstalled”
can u please help me out….my project will remain pending without working on this.
thanks
niki
Note that if you use the jdk7/jdk1.7 keytool, it gives you SHA-1, which is not what you want. I have both jdk6/1.6 and jdk7/1.7 installed, so I just used the jdk1.6 keytool.
There may be a command line option for the 1.7 keytool to have it output md5, but nothing was obvious to me.
Unfortunately It isn’t working for me can you elaborate more. I followed all steps shown above and I found a lot of errors in code. Couldn’t fig. out my mistack.
Very precise tutorial ,Excellent work!
may i know what the problem when the google map did not show the surrounding area ?
thanks alot man , am a newbie though but following the guide step by step which isn’t bad so far, i wonder if i want to send the data via a gps bluetooth device (else then the phone) what should i add on this app ??
Nice work..but i didn’t get the map why..?please anyone give me the suggestion..
What’s the out you are getting Mr.Kanna?
Can you please suggest how to get the current location here. I used the code :
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
OverlayItem overlayitem = new OverlayItem(point,”",”" );
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
But, Then the app crushes automatically.
Thank you for this MapView tutorial! I compiled a list of top resources for adding a MapView to a mobile application. I included your post. Check it out/ feel free to share. http://www.verious.com/board/Giancarlo-Leonio/adding-a-map-view-to-an-android-application/ Hope other developers find it useful too.