Dear readers am back after a long time trying to complete what i started a few months back. This article is part 5 of the previous series but it doesn’t depend on the code of the previous articles, still they can be referenced to get a better understanding of configuring Google Maps for Android and learning the basic development of Google Maps for Android. The development environment stays the same as of the previous articles. No change in any of the configurations etc.
Please see the links for previous articles for basics of Google Maps for Android.
- http://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/
- http://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-google-maps-to-the-current-gps-location-android-tutorial-part-2/
- http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-map-overlays-android-tutorial-part-3/
- http://mirnauman.wordpress.com/2012/02/14/android-google-maps-tutorial-part-4-adding-menu-some-additional-functionality-like-zooming-changing-map-view-animating-to-gps-current-location-using-menu-button/
Objective of this article is to add multiple images to our Google Map Application for Android. I have taken a scenario where I have a list of coordinates of different cities of Pakistan and I want display an image on each city. Check the final output of this article so that you have an idea of what we will achieve at the end of this tutorial.
Starting with the actual work, inside the main GoogleMapsAppActivity Class that extends MapActivity, add a Class lets say MirItemizedClass that extends ItemizedOverlay. This is a base class for an Overlay which consists of a list of OverlayItems. This handles sorting north-to-south for drawing, creating span bounds, drawing a marker for each point, and maintaining a focused item. It also matches screen-taps to items, and dispatches Focus-change events to an optional listener.
private class MirItemizedOverlay extends ItemizedOverlay {
private List<OverlayItem> mOverlays = new ArrayList();
public MirItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
}
We have to set the bounding rectangle of the drawable that we add to our ItemizedOverlay. The defaultMarker is a Drawable that is drawn on every OverlayItem that we add to our ItemizedOverlay. To do this we have to use boundCenterBottom or boundCenter or we can use the setBounds method to get the job done. So we have used boundCenterBottom(defaultMaker). To add multiple overlays we have added a List of OverlayItem and used that in the createItem and size methods of our ItemizedOverlay Class. To add the OverlayItems to our internal list we have added the method addOverlayItem(OverlayItem overlayItem).
Now we will use the functionality of MirItemizedOverlay Class in the onCeate method of our main GoogleMapsAppActivity Class.
We will create instances of Drawable and MirItemizedOverlay to retrieve the image from our res/drawable folder and to supply that image to the our ItemizedOverlay Class.
Drawable makerDefault = this.getResources().getDrawable(R.drawable.star_big_on); MirItemizedOverlay itemizedOverlay = new MirItemizedOverlay(makerDefault); GeoPoint point = new GeoPoint(33480000, 73000000);
We have also ceated a GeoPoint object with a latitude, longitude value. Add another method to our MirItemizedOverlay Class that will take two integers and a string as parameters.
public void addOverlayItem(int lat, int lon, String title) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
}
The code to create and add multiple locations to our itemizedOverlay object by using the recently created method is
OverlayItem overlayItem = new OverlayItem(point, "Islamabad", null); itemizedOverlay.addOverlayItem(33695043, 73000000, "Islamabad"); itemizedOverlay.addOverlayItem(33480000, 73000000, "Some Other Pakistani City"); itemizedOverlay.addOverlayItem(33380000, 73000000, "Some Other Pakistani City");
Now enable the mapView control to display the itemizedOverlay
mapView.getOverlays().add(itemizedOverlay);
Now tweek the mapView to get focused at a certain point and set the zooming level.
MapController mc = mapView.getController(); mc.setCenter(new GeoPoint(33580000, 73000000)); // Some where near Islamabad. mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());
Run the project we will get the output in the picture given above.
Complete Code of this tutorial.
package com.android.googlemapsapp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import android.os.Bundle;
public class GoogleMapsAppActivity extends MapActivity {
private MapView mapView;
private MapController mc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
mapView.setBuiltInZoomControls(true);
Drawable makerDefault = this.getResources().getDrawable(R.drawable.star_big_on);
MirItemizedOverlay itemizedOverlay = new MirItemizedOverlay(makerDefault);
GeoPoint point = new GeoPoint(33480000, 73000000);
OverlayItem overlayItem = new OverlayItem(point, "Islamabad", null);
itemizedOverlay.addOverlayItem(33695043, 73000000, "Islamabad");
itemizedOverlay.addOverlayItem(33480000, 73000000, "Some Other Pakistani City");
itemizedOverlay.addOverlayItem(33380000, 73000000, "Some Other Pakistani City");
mapView.getOverlays().add(itemizedOverlay);
MapController mc = mapView.getController();
mc.setCenter(new GeoPoint(33580000, 73000000)); // Some where near Islamabad.
mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class MirItemizedOverlay extends ItemizedOverlay {
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MirItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
}
}
}
Related articles
- Android Google Maps Tutorial Part 1. Basic Development. (mirnauman.wordpress.com)
- Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Google Maps to the Current GPS location. (mirnauman.wordpress.com)
- Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Map Overlays. (mirnauman.wordpress.com)


40 comments
Comments feed for this article
April 16, 2012 at 9:25 am
Android Google Maps Tutorial Part 6, Getting The Location That Is Touched. « Mir
[...] April 16, 2012 in Android | Tags: Android, Coordinates, GeoPoint, GoogleMap, Islamabad, Location Touched, mapview, onTouchEvent To get going with this tutorial I am using the code from my previous tutorial http://mirnauman.wordpress.com/2012/04/10/android-google-maps-tutorial-part-5-adding-multiple-images… [...]
April 25, 2012 at 7:12 am
Zeeshan
Dear Nauman
I already designed a class with multiple location selection, my question is how to add multiple location information into database? i.e. suppose I want 5 location selected, how am i going to add this info in database
April 25, 2012 at 7:38 am
Mir
Dear Zeeshan,
I ll soon post for creating a Database, Tables and than saving information to those Tables and retrieving that information. Plz check back soon.
May 9, 2012 at 8:01 am
Zeeshan
Dear Nauman,
I have done it.. of course after a lot of trial and errors.
I got it implemented onTap() method. in which par tap i get my class onDraw method to draw png resource on map and in onTap() method I implemented web-service which saves information in database and retrieve information in an other class which passes these coordinates to again onDraw method of that class.
Thanks for posting me back.
Regarrds
Malik Zeeshan.
April 27, 2012 at 4:33 am
Android Google Maps Tutorial Part 2. Using GPS in Android and Animating Google Maps to the Current GPS location. « Mir
[...] Android Google Maps Tutorial Part 5, Adding Multiple Images To Google Maps Using ItemizedOverlay. (mirnauman.wordpress.com) Share this:TwitterLinkedInFacebookEmailPrintLike this:LikeBe the first to like this post. Categories [...]
April 27, 2012 at 4:34 am
Android Google Maps Tutorial Part 3. Adding An Image to GoogleMaps Using Map Overlays. « Mir
[...] Android Google Maps Tutorial Part 5, Adding Multiple Images To Google Maps Using ItemizedOverlay. (mirnauman.wordpress.com) [...]
May 9, 2012 at 3:56 am
Mir
Reblogged this on Mir.
July 12, 2012 at 3:02 pm
zeeshan
getApplicationContext is undefined , I did not find in your tutorial please guide me.
August 1, 2012 at 4:18 pm
Kenny
Dear Mir. I have finished your code, it’s so great. But how to show address name above every location icon?
August 2, 2012 at 3:42 am
Mir
dear kenny,
if u r referring to names of location on the exact coordinates where we have placed our icons, we can use reverse geocoding. displaying those names on top of each icon will be a bit trick. i ll try to write something about it soon. for geocoding and reverse geocoding plz go through my article http://mirnauman.wordpress.com/2012/06/07/android-google-maps-tutorial-part-8-geocoding-and-reverse-geocoding/
August 2, 2012 at 1:07 pm
Kenny
I will wait for your post, but I still have a question.
I wanna find the method to show a shortest direction road between two Geopoints (not a straight line). Can you show me some documents about this?
Thanks Mir
August 27, 2012 at 11:49 pm
Toni
First of all thank you very much for the complete and clear tutorials in the google maps topic. I am creating an app for a school project of mine and i found these tutorials very helpful but of course i had smth to ask you
1st: Making all these classes part of the same file is smth mandatory in order to exploit the context of googleMapsActivity class?
2nd:My idea in the project is to search for 2 different locations of interest in the map. I want to create 2 diff markers for the various points and depending on what the user wants to search for , display the respective markers. IS this possible ? If yes , do I need 2 diff overlays or can i work with various methods?
Thx for any response and i’m sorry if the questions are too obvious. (i’m a beginner in android
)
August 30, 2012 at 11:57 am
Mir
its not mandatory to make all classes part of the same file. but i have done it so that my readers can easily understand the idea and flow.
u can display as many markers as u want and u don’t need 2 overlays, one is enough. secondly searching two location is possible if u have the coordinates. u can easily put markers on those coordinates.
September 2, 2012 at 11:36 am
mustafa
Dear Mir i apply your code in my computer its work but when i use it in my phone not show me the map why ?could you help me please.
September 4, 2012 at 4:30 am
Mir
Check the android version than check the google Maps Api versions on ur phone and on ur emulator. secondly if the device loads the map but the screen is empty grid than that is a problem of Google Maps APi Key.
September 5, 2012 at 7:19 pm
mustafa
Thank sir to answer me i Check my android version and the google Maps Api versions its same but the problem not show me the map just the star and if you ask me about android key i register it in Google map after get MD5 .what do you suggest to me I do.
September 6, 2012 at 4:42 am
Mir
dear mustafa,
empty map grid as far as i know is a problem of google maps api key, check the following links,
http://mirnauman.wordpress.com/2012/01/26/how-to-get-google-maps-api-key-for-android-issues-and-errors-solved/
and than
http://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/
i hope if u follow each and everything exactly, it will solve ur issues.
September 6, 2012 at 6:50 pm
mustafa
Thank sir you support me but i discovered what my fault after export to android apk not the same bath and i current this ,so now every think is ok
i know i tired you but can you help me hoe to save this location
( 33695043, 73000000, “Islamabad”);
(33480000, 73000000, “Some Other Pakistani City”);
(33380000, 73000000, “Some Other Pakistani City”);
in data base i want to bottom search for any location for example write in this bottom Islamabad .the result show me just one star.
With my best wishes .
September 7, 2012 at 4:56 am
Mir
Dear mustafa,
saving data on device in database or in flat files is a completely separate domain. the above series or articles only show the basics for google maps for android. i have written application on saving data in databases as well as in flat text files. but have not written tutorial for them yet. i ll try to publish their tutorial ASAP.
March 15, 2013 at 12:25 pm
Maimoonah
If you have published the tutorials to save in database please put a link here..Thanks
September 7, 2012 at 9:04 am
mustafa
thank you sir for this useful information and thanks very much for your kindness and humility . I’m waiting this tutorial .thank you again
September 13, 2012 at 6:53 pm
mustafa
hi Mir how are you i hope you are fine. Sir i want ask you how can i show my location automatically on the same Google map(Tutorial Part 5) i mean use Gps . For example i show my current location when i go to any place at Luminous arrow
September 22, 2012 at 12:32 pm
daryna_kvitka (@bohemiene)
Hello! Thenk you so much for this tutorial
I have a question about adding photos to google maps. How is it possible to add a photo which is sored on the phone to the map? I would be grateful for the link on methods how it is possible. Thank you.
September 24, 2012 at 6:10 am
Mir
dear kavita
to add an image stored on the phone u need to follow the following article step by step.
http://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-map-overlays-android-tutorial-part-3/
this article shows basics of adding images to google maps.
September 24, 2012 at 8:33 pm
daryna_kvitka (@bohemiene)
Thank you for teh answer. This tutorial was very helpful
I want the user to choose the photo from teh photos which are stored on the phone and to upload this chosen photo. In the tutorial there was this specific photo. Or am I mistaking?
September 25, 2012 at 3:38 am
Mir
well the above tutorial’s main focus is on the basic functionality of google maps and not reading contents(images,text files etc) from any location(photo galary, sd card etc) from the device. thats why i have copied the image in the package and hardcoded it in the source. to dynamically get contents from any location from the device will need an all new tutorial. i ll write something about it. soon. Each article here will not provide you everything that u need, but chunks that can be used from different programs to get wat u need. I ll write something about reading contents dynamically from the device soon. this could be nice thing to write about. thanks for pointing it out.
September 26, 2012 at 6:26 pm
Muiz
can i have two types of overlays 1 for my current location and the other of places ? and can i use sqlite to store the places?
September 27, 2012 at 10:11 am
Mir
i have not used two overlays so far, but i think one can do the job than why use two. still u can give it a try and post ur experience under this thread so that every1 knows. secondly yes u can save the places in sqlite or simple comma seperated txt files or xml files. depends wat u will prefer and how u will retrieve data back from them.
September 27, 2012 at 8:03 am
mustafa
Thank you, Mir, on these notes
October 5, 2012 at 6:31 am
Amit Suri
Mir Nauman Tahir, i have tried your code and after few changes able to run and getting each and everything, but need to add location name whenever user touch an overlay item, how can we add location name on overlay items…..Thanks
October 22, 2012 at 6:00 am
ashana jackol
mapactivity could’nt get connection factory client ,can you help me for this error,when i open the app map comming out and didn’t show the overlays that i added,bt in logcat i got the real output,only thing is didnt visible at the map.plzzz hlp
December 20, 2012 at 8:13 am
Sergey
Good afternoon. Thanks for the tutorial, it works. Posdkazhite, please, is it possible somehow to add markers, if one is not aware of them? That eats create a variable, for example, the user will select a number of 5 or 10, and the show, or 5, or 10 markers. Is this possible?
December 21, 2012 at 4:29 pm
mustafa
Hi , just I am wondering what benefit for this tutorial if anyone can add markers on google markers ,, still I have confused . can anyone tell me what is the different between them .
January 17, 2013 at 7:30 am
Uttam
Hi mir, This tutorial is very nice, i exctly looking for that kind of turorial, can we identifed which image is clicked on the map ! Thank You!
March 3, 2013 at 5:32 pm
Drawing A Path or Line Between Two Locations (Google Maps in Android) » Muhammad Yusro
[...] Android Google Maps Tutorial Part 5, Adding Multiple Images To Google Maps Using ItemizedOverlay. (mirnauman.wordpress.com) [...]
March 4, 2013 at 7:12 am
Choi
can that image be an ImageButton and launch another activity?
March 14, 2013 at 9:35 pm
Maimoonah
Thanks..
I want to draw a rote where the user is during trip,,,but I couldn’t find a way..So I will try to create the route according to the multiple cursor(make the cursor circle image or point) ..So I’ve included GPS ,So the emulator draws but not all times …Can you help me?
April 16, 2013 at 7:27 am
Nestoras
I have my latitude and longitute in double, i have to multiply them for make it an integer or it will not work?
April 16, 2013 at 7:52 am
Mir
why not parsing the figures.
April 16, 2013 at 8:03 am
Nestoras
it’s ok, i have solve the problem with this ((int) (lat*1E6),(int) (lng*1E6))
Thank you