Get Current Address in Android using Geocoder

A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. 


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/backgd">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Lattitude :"
        android:id="@+id/latti"
        android:textColor="#FF0000"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp" android:textSize="18dp"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Longitude :"
        android:textColor="#FF0000"
        android:id="@+id/longi"
        android:textSize="18dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"/>
 <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Address :"
        android:textColor="#FF0000"
        android:id="@+id/address"
        android:textSize="18dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp" />
</LinearLayout>

CurrentAddressActivity.java

public class CurrentAddressActivity extends Activity {
static List<Address> addresses;
boolean IS_GPSAVAILABLE = false;
static GeoPoint point = null;
static Location currentLocations;
static double latitude, longitude;
String address;
private LocationManager locationManager;
private LocationListener locationListener;
String latti;
String longi;
TextView lattit;
TextView longit;
TextView addre;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lattit = (TextView)findViewById(R.id.latti);
        longit = (TextView)findViewById(R.id.longi);
        addre  = (TextView)findViewById(R.id.address);
       
        locationUpdates();
setCurrentLocation();

lattit.setText("Lattitude : "+ latti);
longit.setText("Longitude :" + longi);
addre.setText("Address : " + address);


    }
   
    private void locationUpdates() {

try {
IS_GPSAVAILABLE = true;
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

} catch (Exception e) {
IS_GPSAVAILABLE = false;
}
}

protected void setCurrentLocation() {
try {
IS_GPSAVAILABLE = true;
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (location != null) {
point = getPoint(location.getLatitude(),
location.getLongitude());
currentLocations = location;

latitude = location.getLatitude();
longitude = location.getLongitude();

address = ConvertPointToLocation(point);
System.out.println("Address point::::" + address);
latti = Double.toString(latitude);
longi = Double.toString(longitude);
}



} catch (Exception e) {
// TODO Auto-generated catch block
IS_GPSAVAILABLE = false;
}
}

private String ConvertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(CurrentAddressActivity.this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6,
1);

if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0)
.getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
} catch (IOException e) {
e.printStackTrace();
}

return address;
}

public static GeoPoint getPoint(double lat, double lon) {

return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}

public class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// setCurrentLocation();
}

public void onStatusChanged(String s, int i, Bundle b) {

}

public void onProviderDisabled(String s) {

}

public void onProviderEnabled(String s) {

}

}
}

in the manifest file:
add the below permissions:

<uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
    </uses-permission>
add uses-library inside the application tag:
<uses-library android:name="com.google.android.maps" />