Convert Address To Name - Android android.location

Android examples for android.location:Address

Description

Convert Address To Name

Demo Code

import android.location.Address;
import android.text.TextUtils;
import android.util.Log;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    private static Pattern BAD_DESCRIPTION_RE = Pattern
            .compile("[\\d\\s-]+");
    public final static String TAG = "";
    public static String addressToName(Address address) {

        final Address thisLocation = address;

        String title = thisLocation.getFeatureName();

        final Matcher m = BAD_DESCRIPTION_RE.matcher(title == null ? ""
                : title);//  w  ww . j  ava2  s  . co  m

        if (TextUtils.isEmpty(title) || m.matches()) {
            title = thisLocation.getAddressLine(0);
        }

        String locality = thisLocation.getLocality();
        String country = thisLocation.getCountryName();

        if (TextUtils.isEmpty(locality)) {
            locality = thisLocation.getAdminArea();
        }

        if (TextUtils.isEmpty(country)) {
            country = thisLocation.getCountryCode();
        }

        if (TextUtils.isEmpty(title) || m.reset(title).matches()) {
            title = thisLocation.getSubLocality();
        }

        if (TextUtils.isEmpty(title) || m.reset(title).matches()) {

            title = (locality != null ? locality + ", " : "") + country;
        } else {
            title = (TextUtils.isEmpty(title) ? "" : title + ", ")
                    + locality;
        }

        return title;
    }

}

Related Tutorials