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.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

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) {
        super(boundCenterBottom(marker));
    }

    public MyItemizedOverlay(Drawable marker, Context context) {
        super(boundCenterBottom(marker));
        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 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);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        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.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.*;

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. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        mapView = (MapView) findViewById(R.id.mapview);

        //sets the zoom to see the location closer
        mapView.getController().setZoom(12);

        //this will let you to zoom in or out using the controllers
        mapView.setBuiltInZoomControls(true);

        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)
        mapView.getController().setCenter(point);
        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");

        itemizedoverlay.addOverlay(overlayitem);
        itemizedoverlay.addOverlay(overlayitem2);
        mapOverlays.add(itemizedoverlay);
    }

    @Override
    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

mapView.getController().setCenter(point);

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

Menu