Add marker on touched location using Google Map in Android – Example

September 30, 2012

In this article, we will create an Android application which adds a marker on the touched location of the Google Map. It also displays the latitude and longitude of the touched location in a TextView.

This application is developed in Eclipse ( 4.2.0 ) with ADT plugin ( 20.0.3 ) and Android SDK ( R20.0.3 ).

Update : An upgraded version of this application ( using Google Map Android API V2 ) is available in the article titled “Adding marker on touched location of Google Maps using Android API V2 with SupportMapFragment

1. Create a new Android application project namely “LocationAtTouchedPosition”

Create a new Android application project

Figure 1 : Create a new Android application project

2. Design application launcher icon

Design application launcher icon

Figure 2 : Design application launcher icon

3. Create a blank activity to define the class MainActivity

Create a blank Activity

Figure 2 : Create a blank Activity

4. Enter MainActivity details

Enter MainActivity Details

Figure 4 : Enter MainActivity Details

5. Delete Android’s backward compatibility support library from this project, if exists

By default Eclipse ( 4.2.0) adds Android Support Library to  Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”.

6. Obtain Google Map Key

Google’s Map Key for the Android application will be available here.

The map key obtained has to be entered as the value of the attribute android:apiKey in the layout file res/layout/activity_main.

7. Create a new folder namely drawable under the folder “res”

8. Download the given below image file namely “marker.png” to the folder “res/drawable”

9. Update the file res/values/strings.xml

    <string name="app_name">LocationAtTouchedPosition</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">Location At Touched Position</string>

10. Update the file res/layout/activity_main.xml

    android:layout_height="match_parent" >

        tools:context=".MainActivity" />

        android:layout_below="@id/tv_location" />

Note : Replace the value of android:apiKey with the Google Map Key obtained in Step 6

11. Create a class namely TouchedLocationOverlay in the file src/in/wptrafficanalyzer/locationattouchedposition/

package in.wptrafficanalyzer.locationattouchedposition;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


public class TouchedLocationOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Handler handler;

    public TouchedLocationOverlay(Drawable defaultMarker,Handler h) {

        // Handler object instantiated in the class MainActivity
        this.handler = h;

    // Executed, when populate() method is called
    protected OverlayItem createItem(int arg0) {
        return mOverlays.get(arg0);

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

    public void addOverlay(OverlayItem overlay){
        populate(); // Invokes the method createItem()

    // This method is invoked, when user tap on the map
    public boolean onTap(GeoPoint p, MapView map) {

        List<Overlay> overlays = map.getOverlays();

        // Creating a Message object to send to Handler
        Message message = new Message();

        // Creating a Bundle object ot set in Message object
        Bundle data = new Bundle();

        // Setting latitude in Bundle object
        data.putInt("latitude", p.getLatitudeE6());

        // Setting longitude in the Bundle object
        data.putInt("longitude", p.getLongitudeE6());

        // Setting the Bundle object in the Message object

        // Sending Message object to handler

        return super.onTap(p, map);

12. Update the class MainActivity in the file src/in/wptrafficanalyzer/locationattouchedposition/

package in.wptrafficanalyzer.locationattouchedposition;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends MapActivity {

    private MapView mapView;
    private TextView tvLocation;

    // Handles Taps on the Google Map
    Handler h = new Handler(){

        // Invoked by the method onTap()
        // in the class CurrentLocationOverlay
            public void handleMessage(Message msg) {
            Bundle data = msg.getData();

            // Getting the Latitude of the location
            int latitude = data.getInt("latitude");

            // Getting the Longitude of the location
            int longitude = data.getInt("longitude");

            // Show the location in the Google Map

    public void onCreate(Bundle savedInstanceState) {

        // Getting reference to map_view available in activity_main.xml
        mapView = (MapView) findViewById(;

        // Getting reference to tv_location available in activity_main.xml
        tvLocation = (TextView) findViewById(;

        // Default Latitude
        int latitude = 28426365;

        // Default Longitude
        int longitude = 77320393;

        // Show the location in the Google Map

    private void showLocation(int latitude, int longitude){

        // Setting Latitude and Longitude in TextView
        tvLocation.setText("Latitude:" + latitude/1e6 + "," + "Longitude:" + longitude/1e6);

        // Setting Zoom Controls

        // Getting the MapController
        MapController mapController = mapView.getController();

        // Getting Overlays of the map
        List<Overlay> overlays = mapView.getOverlays();

        // Getting Drawable object corresponding to a resource image
        Drawable drawable = getResources().getDrawable(R.drawable.marker);

        // Creating an ItemizedOverlay
        TouchedLocationOverlay locationOverlay = new TouchedLocationOverlay(drawable,h);

        // Getting the MapController
        MapController mc = mapView.getController();

        // Creating an instance of GeoPoint, to display in Google Map
        GeoPoint p = new GeoPoint(

        // Locating the point in the Google Map

        // Creating an OverlayItem to mark the point
        OverlayItem overlayItem = new OverlayItem(p, "Item", "Item");

        // Adding the OverlayItem in the LocationOverlay

        // Clearing the overlays

        // Adding locationOverlay to the overlay

        // Redraws the map


    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return false;

        protected boolean isRouteDisplayed() {
            // TODO Auto-generated method stub
            return false;

13. Update the file AndroidManifest.xml

<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET"/>

        android:theme="@style/AppTheme" >

        <uses-library android:name=""/>

            android:label="@string/title_activity_main" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

14. Screenshot of the application

Showing touched Position in the Google Map

Figure 5 : Showing Location at touched Position in Google APIs emulator for API Level 4

15. Download

16. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , , ,

5 Responses to Add marker on touched location using Google Map in Android – Example

  1. gus on October 20, 2012 at 4:56 am

    THANKS men!
    very useful

  2. brij on December 14, 2012 at 10:03 pm

    I am using your code in my project , but i need to display marker at current location as well as touched location in the same map …please help me by sending solution ,I have few day for my project.

  3. sefin on February 4, 2013 at 10:16 pm

    I am using your code and modified a little… Thank you so much guys, Your tutorial is so helpfull to my college duty… :)

  4. AYAN SINHA on March 5, 2013 at 2:33 pm

    Hi George,

    Can you tell me after you added marker dynamically, now how to use AlertDialog on tap of that. As I am facing difficulties in adding them(alertdialog), it gives me error. Please help and thanks for your article.

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner