Android How to put markers/pins on the map

Android How to put markers/pins on the map

We’ve already explained how to make an application with a map here. So, now we will try to put markers or pins (or however are they called) on the map, by knowing the Latitude and Longitude, in case you will ever need it and someday you will 🙂 . So lets start:

1. After you create your Android project you have to create another class besides the main one. Let’s call it MyItemizedOverlay. Well after you made this you must extend ItemizedOverlay class and override some methods. The code is just as bellow:

package com.example;

import android.content.Context;

import java.util.ArrayList;

 * Description
 * @author : kazy
 *         Date: 3/23/12
public class MyItemizedOverlay extends ItemizedOverlay {
    private ArrayList< overlayitem > mOverlays = new ArrayList< overlayitem >();
    Context mContext;

    public MyItemizedOverlay(Drawable marker) {

    public MyItemizedOverlay(Drawable marker, Context context) {
        mContext = context;

    public void addOverlay(OverlayItem overlay) {

    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);

    public int size() {
        return mOverlays.size();

    protected boolean onTap(int i) {
        //when you tap on the marker this will show the informations provided by you when you create in the 
        //main class the OverlayItem
        OverlayItem item = mOverlays.get(i);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        return true;

2. Now you have to get a marker image (you can find it on google images) and copy it into the drawable folder (you should name your marker image marker.png like we did for simplicity). If you don’t have a drawable folder press right click on the res directory and create a folder/directory called “drawable”.

3. After you got the image you can go to your main activity class(mine is called MyActivity and extends MapActivity, not Activity don’t forget about this) and put the following code:

package com.example;

import android.os.Bundle;

import java.util.List;

public class MyActivity extends MapActivity

    private MapView mapView;
    private MapController mapController;
    private GeoPoint geoPoint;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)

        mapView = (MapView) findViewById(;

        //sets the zoom to see the location closer

        //this will let you to zoom in or out using the controllers

        List< overlay > mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker);

        MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);

        GeoPoint point = new GeoPoint(46066940, 23570000);
       //this will show you the map at the exact location you want (if you not set this you will see the map somewhere in America)
        OverlayItem overlayitem = new OverlayItem(point, "Title for dialog", "Alba Iulia City From Romania");

        GeoPoint point2 = new GeoPoint(35410000, 139460000);
        OverlayItem overlayitem2 = new OverlayItem(point2, "Title for dialog", "Japan");


    protected boolean isRouteDisplayed() {
        return false;

And now lets see the result of this code 🙂

Now as you can see we have 2 locations(point and point2). To first one is shown to us because we set here


So to see the other location which is very far away (in Japan) you need to move the map.

NOTE: The code is mostly from the android developer site

, ,
Previous Post
Android PreferenceActivity & SharedPreferences tutorial
Next Post
Run Android Lint from command line

Related Posts