Android で緯度/経度から住所を取得する方法

Android で緯度/経度から住所を取得する方法

Takahiro Iwasa
Takahiro Iwasa
1 min read
Android GIS Java

android.location.Geocoder を使用すると、緯度/経度から住所を取得できます。

/**
 * 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.