Android Google MapView Tutorial — Done Right!

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.

Android SDK & AVD Manager

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 androidDon’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”

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.

Adding the HelloItemizedOverlay 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):

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.

Create Google Maps AVD

Now that all the code is in place, you can run the application:
Right click on the project and choose Run As Android Application

Run Application

This is what you should see (provided all the code was entered error free and the API Keys were entered properly):

Overview Result

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!

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.

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!

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.
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 :D Took me a while to figure it out … Bummer

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)

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

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

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?

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.

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.

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

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

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.

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

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.

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.

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?

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

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

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

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