Android Reverse Geocoding at touched location in Google Map Android API V2

January 9, 2013

In this article we will create an Android application which resolves (partial) address from the latitude and longitude of the touched position of the Google Map Android API V2.

The process of resolving location address from latitude and longitude is called reverse Geocoding and for this we will make use Google’s Geocoder library API.

This application is developed in Eclipse 4.2.1 with ADT plugin ( 21.0.0 ) and Android SDK ( 21.0.0 ) and is tested in a real Android Phone with Android ( 2.3.6 ).

This application is an upgraded version of the application developed in the article titled “Android Reverse Geocoding using Geocoder at touched Location in Google Map“.

1. Download and configure Google Play Services Library in Eclipse

Please follow the given below link to setup Google Play Service library in Eclipse.

2. Create a new Android Application Project namely “LocationReverseGeocodingV2″

New Android Application Project

Figure 1 : New Android Application Project

3. Configure Android Application Project

Configure Application Project

Figure 2 : Configure Application Project

4. Design Application Launcher Icon

Design application launcher icon

Figure 3 : Design application launcher icon

5. Create a blank activity

Create a blank activity

Figure 4 : Create a blank activity

6. Enter Main Activity Details

Enter Main Activity Details

Figure 5 : Enter Main Activity Details

7. Link to Google Play Service Library

Link Google Play Services Library

Figure 6 : Link Google Play Services Library

8. Get the API key for Google Maps Android API v2

We need to get an API key from Google to use Google Maps in Android application. Please follow the given below link to get the API key for Google Maps Android API v2.

9. Add Android Support library to this project

By default, Android support library (android-support-v4.jar ) is added to this project by Eclipse IDE to the directory libs. If it is not added, we can do it manually by doing the following steps :

  • Open Project Explorer by Clicking “Window -> Show View -> Project Explorer”
  • Right click this project
  • Then from popup window, Click “Android Tools -> Add Support Library “

10. Update the file AndroidManfiest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="16" />


    <uses-permission android:name="in.wptrafficanalyzer.locationreversegeocodingv2.permission.MAPS_RECEIVE"/>

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


        android:theme="@style/AppTheme" >

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


Note : In the above code, replace “YOUR_API_KEY” with the api key, you generated in step 8.

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

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

    <fragment xmlns:android=""


12. Update the file src/in/wptrafficanalyzer/locationreversegeocodingv2/

package in.wptrafficanalyzer.locationreversegeocodingv2;

import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;


public class MainActivity extends FragmentActivity {

    GoogleMap googleMap;
    MarkerOptions markerOptions;
    LatLng latLng;

    protected void onCreate(Bundle savedInstanceState) {

        SupportMapFragment supportMapFragment = (SupportMapFragment)

        // Getting a reference to the map
        googleMap = supportMapFragment.getMap();

        // Setting a click event handler for the map
        googleMap.setOnMapClickListener(new OnMapClickListener() {

            public void onMapClick(LatLng arg0) {

                // Getting the Latitude and Longitude of the touched location
                latLng = arg0;

                // Clears the previously touched position

                // Animating to the touched position

                // Creating a marker
                markerOptions = new MarkerOptions();

                // Setting the position for the marker

                // Placing a marker on the touched position

                // Adding Marker on the touched location with address
                new ReverseGeocodingTask(getBaseContext()).execute(latLng);


    private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
        Context mContext;

        public ReverseGeocodingTask(Context context){
            mContext = context;

        // Finding address using reverse geocoding
        protected String doInBackground(LatLng... params) {
            Geocoder geocoder = new Geocoder(mContext);
            double latitude = params[0].latitude;
            double longitude = params[0].longitude;

            List<Address> addresses = null;
            String addressText="";

            try {
                addresses = geocoder.getFromLocation(latitude, longitude,1);
            } catch (IOException e) {

            if(addresses != null && addresses.size() > 0 ){
                Address address = addresses.get(0);

                addressText = String.format("%s, %s, %s",
                address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",

            return addressText;

        protected void onPostExecute(String addressText) {
            // Setting the title for the marker.
            // This will be displayed on taping the marker

            // Placing a marker on the touched position


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

13. Screenshot of the application in execution

Showing address of the touched position using Google's Geocoder API

Figure 7 : Showing address of the touched position using Google’s Geocoder API

14. Download Source Code

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: , , , , ,

11 Responses to Android Reverse Geocoding at touched location in Google Map Android API V2

  1. Kijoo Nguyen on March 9, 2013 at 9:07 pm

    This doesn’t work for me.
    The map works fine but the address return is blank

  2. swathi on March 16, 2013 at 1:52 pm

    This doesn’t work for me.
    The map works fine but the address return is null in the place name

  3. pratik on April 20, 2013 at 3:20 pm

    Thanx for your valuable post but there is 1 problem..can you please help me…i have done google search but couldn’t find any solution. Service not Available

    it throws exception on this line in your code
    addresses = geocoder.getFromLocation(latitude, longitude,1);

    any idea?

  4. Hoon Lee on May 9, 2013 at 11:21 am

    Thank you for this source. I take advantage of it a lot. Anyway I have tried this one, it works but address dialog doesn’t appear on the map. I really appreciate if you give me some hint for it.

  5. harishkumar on January 11, 2014 at 1:32 pm

    I am really sorry for last comment …and Thank you Its working for me …it takes some to fetch address ..Working fine

  6. suresh on March 21, 2014 at 1:25 pm

    can i add weather layer in mapversion v2 with my current location

  7. Manish Sindhal on August 29, 2014 at 3:26 pm

    It’s great work for me but some changes in show address in marker.


  8. Alicia Beltran on November 4, 2014 at 9:33 am

    Hi, somethink wrong is happening.

    11-03 22:47:17.524: E/AndroidRuntime(30376): FATAL EXCEPTION: AsyncTask #3
    11-03 22:47:17.524: E/AndroidRuntime(30376): Process: in.wptrafficanalyzer.locationwaypointmapv2, PID: 30376
    11-03 22:47:17.524: E/AndroidRuntime(30376): java.lang.RuntimeException: An error occured while executing doInBackground()
    11-03 22:47:17.524: E/AndroidRuntime(30376): at android.os.AsyncTask$3.done(
    11-03 22:47:17.524: E/AndroidRuntime(30376): at java.util.concurrent.FutureTask.finishCompletion(
    11-03 22:47:17.524: E/AndroidRuntime(30376): at java.util.concurrent.FutureTask.setException(
    11-03 22:47:17.524: E/AndroidRuntime(30376): at
    11-03 22:47:17.524: E/AndroidRuntime(30376): at android.os.AsyncTask$SerialExecutor$
    11-03 22:47:17.524: E/AndroidRuntime(30376): at java.util.concurrent.ThreadPoolExecutor.runWorker(
    11-03 22:47:17.524: E/AndroidRuntime(30376): at java.util.concurrent.ThreadPoolExecutor$
    11-03 22:47:17.524: E/AndroidRuntime(30376): at
    11-03 22:47:17.524: E/AndroidRuntime(30376): Caused by: java.lang.NullPointerException
    11-03 22:47:17.524: E/AndroidRuntime(30376): at in.wptrafficanalyzer.locationwaypointmapv2.MainActivity$ReverseGeocodingTask.doInBackground(
    11-03 22:47:17.524: E/AndroidRuntime(30376): at in.wptrafficanalyzer.locationwaypointmapv2.MainActivity$ReverseGeocodingTask.doInBackground(
    11-03 22:47:17.524: E/AndroidRuntime(30376): at android.os.AsyncTask$
    11-03 22:47:17.524: E/AndroidRuntime(30376): at
    11-03 22:47:17.524: E/AndroidRuntime(30376): … 4 more

    I didnt identify what is happenning with those theat.

    Any suggestion?

  9. Abdul Khalik on June 9, 2015 at 5:01 pm

    Thans for your most valuable post. Google map is showing very well but address doesn’t return so plz help me how can I get address on map touch

  10. Abdul Khalik on June 16, 2015 at 11:47 am

    Solution: We will have to take a TextView in xml like that:

    and in MainActivity class initialize TextView and then set in post execute method like that:

    protected void onPostExecute(String addressText) {
    // Setting address of the touched Position


    Your code will work 101% fine do enjoy this…

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