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. Happy Coding!

/**
 * 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 requirements definition, design, and development of cloud-native applications using AWS. Now, building a new prescription data collection platform at KAKEHASHI Inc. Japan AWS Top Engineers 2020-2023.