Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.support.annotation.NonNull; import android.text.TextUtils; import java.io.IOException; import java.util.List; import java.util.Locale; public class Main { public static String getAddressFromLocation(@NonNull Context context, @NonNull Location location) { final Geocoder geocoder = new Geocoder(context, Locale.getDefault()); final StringBuilder builder = new StringBuilder(); try { final List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5 // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex() final String address = addresses.get(0).getAddressLine(0); if (addIfNotEmpty(builder, address)) { builder.append(", "); } final String city = addresses.get(0).getLocality(); if (addIfNotEmpty(builder, city)) { builder.append(", "); } final String postalCode = addresses.get(0).getPostalCode(); if (addIfNotEmpty(builder, postalCode)) { builder.append(", "); } final String state = addresses.get(0).getAdminArea(); if (addIfNotEmpty(builder, state)) { builder.append(", "); } final String country = addresses.get(0).getCountryName(); if (addIfNotEmpty(builder, country)) { builder.append(", "); } // final String knownName = addresses.get(0).getFeatureName(); // addIfNotEmpty(builder, knownName); } catch (IOException e) { e.printStackTrace(); } return builder.toString(); } private static boolean addIfNotEmpty(StringBuilder builder, String stringToCheck) { if (!TextUtils.isEmpty(stringToCheck)) { builder.append(stringToCheck); return true; } return false; } }