Recipe Translating a Location to Address Reverse Geocoding

The Geocoder class provides a method to translate from an address into a latitude-longitude coordinate (geocoding) and from a latitude-longitude coordinate into an address (reverse geocoding). Reverse geocoding might produce only a partial address, such as city and postal code, depending on the level of detail available to the location provider.

This recipe uses reverse geocoding to get an address from the device's location and display to the screen, as shown in Figure 10.2.The Geocoder instance needs to be initiated with a context and optionally with a locale if different from the system locale. Here, it is explicitly set to Locale.ENGLISH.Then the getFromLocation() method provides a list of addresses associated with the area around the provided location. Here the maximum number of returned results is set to one (for instance, the most likely address).

The Geocoder returns a List of android.location.Address objects.This translation to an address depends on a backend service that is not included in the core Android framework. The Google Maps API provides a client Geocoder service, for example. However, the translation returns an empty list if no such service exists on the target device. The address as a list of strings is dumped line by line into a String for display on the screen.The main activity is shown in Listing 10.6.

mylocat mylocation


2953 Bunker Hlll Lr Santa Clara, CA 95054

Figure 10.2 Reverse geocoding example, which converts a latitude-longitude coordinate into an address.

Listing 10.6 src/com/cookbook/rev_geocoding/MyLocationjava package com.cookbook.rev_geocoding;

import; import java.util.List; import java.util.Locale;

import; import android.content.Context; import android.location.Address; import android.location.Criteria; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.widget.TextView;

public class MyLocation extends Activity { LocationManager mLocationManager; Location mLocation; TextView tv;

^Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState);


tv = (TextView) findViewById(;

mLocationManager = (LocationManager)


Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_LOW); String locationprovider =


mLocation =


List<Address> addresses; try {

Geocoder mGC = new Geocoder(this, Locale.ENGLISH); addresses = mGC.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1);

Address currentAddr = addresses.get(0); StringBuilder mSB = new StringBuilder("Address:\n"); for(int i=0; i<currentAddr.getMaxAddressLineIndex(); i++) { mSB.append(currentAddr.getAddressLine(i)).append("\n");


} catch(IOException e) {


Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


  • Rasmus
    How to convert address to latitude longitude in android?
    7 years ago

Post a comment