How to Get Address from Latitude/Longitude in Android

How to Get Address from Latitude/Longitude in Android

Takahiro Iwasa
(岩佐 孝浩)
Takahiro Iwasa (岩佐 孝浩)
1 min read
Android GIS Java

Addresses can be obtained from latitude/longitude using android.location.Geocoder in Android.

/**
 * Get address from latitude/longitude.
 * @param context
 * @param latitude
 * @param longitude
 * @return Address
 */
public static String getAddress(
        Context context, double latitude, double longitude) {

    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    List<Address> addresses;
    StringBuilder result = new StringBuilder();

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

    for (Address address : addresses) {
        int idx = address.getMaxAddressLineIndex();
        // Omit the first record including a country name.
        for (int i = 1; i <= idx; i++) {
            result.append(address.getAddressLine(i));
        }
    }

    return result.toString();
}
Takahiro Iwasa
(岩佐 孝浩)

Takahiro Iwasa (岩佐 孝浩)

Software Developer at KAKEHASHI Inc.
Involved in the design, development, and operation of the prescription data collection platform