get Address From Location - Android android.location

Android examples for android.location:Address

Description

get Address From Location

Demo Code

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import java.io.IOException;
import java.util.List;

public class Main{

    private static Geocoder sGeocoder;
    public static String getAddressFrom(Context context, Location location) {
        if (sGeocoder == null) {
            sGeocoder = new Geocoder(context);
        }//  ww  w.  j  a va  2  s .  co  m

        try {
            List<Address> listAddress = sGeocoder.getFromLocation(
                    location.getLatitude(), location.getLongitude(), 1);
            if (listAddress != null && listAddress.size() > 0) {
                Address address = listAddress.get(0);
                int numOfAddressLine = address.getMaxAddressLineIndex();
                String result = "";
                for (int i = 0; i < numOfAddressLine; ++i) {
                    result += address.getAddressLine(i) + " ";
                }

                return result;
            }
        } catch (IOException e) {
        }

        return null;
    }

}

Related Tutorials