Android で緯度/経度から住所を取得する方法
Takahiro Iwasa
1 min read
Android GIS Java
It has been more than 3 years since this post was published.
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();
}