How to format the address for multi-line display?

Like this question from 2011, in which there is no satisfactory answer:

The application I'm working on will be deployed internationally. The application itself only cares about the length of the Addresslat / long, but it will need to display Addressfor the user in a multi-line format. Google geocoder provides a formatted address, but it is on the same line, separated by commas. Dividing this into lines will require knowledge of how multi-line addresses are formatted in a given country. For example, in the USA it is customary to place a city and state on the same line, separated by a comma.

Is there a built-in method (or a third-party library or web service) that will format a multi-line address from Address, given that the reverse geocoded addresses may be incomplete?

+4
source share
1 answer

Check out googlei18n / libaddressinput: Googles mail address library including Android and Chromium . There are two modules in the project: android and: general. You only need: general to format the address for multi-line display.

import android.location.Address;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.google.i18n.addressinput.common.AddressData;
import com.google.i18n.addressinput.common.FormOptions;
import com.google.i18n.addressinput.common.FormatInterpreter;
...
public static String getFormattedAddress(@NonNull final Address address, 
                                         @NonNull final String regionCode) {
    final FormatInterpreter formatInterpreter
            = new FormatInterpreter(new FormOptions().createSnapshot());
    final AddressData addressData = (new AddressData.Builder()
            .setAddress(address.getThoroughfare())
            .setLocality(address.getLocality())
            .setAdminArea(address.getAdminArea())
            .setPostalCode(address.getPostalCode())
            .setCountry(regionCode) // REQUIRED
            .build());
    // Fetch the address lines using getEnvelopeAddress,
    List<String> addressFragments = formatInterpreter.getEnvelopeAddress(addressData);
    // join them, and send them to the thread.
    return TextUtils.join(System.getProperty("line.separator"),
            addressFragments);
}

NOTE. regionCode must be a valid iso2 country code because the format interpreter format is used here. (See RegionDataConstants for a list of formats if you're interested.)

2- CLDR ; AddressData # getPostalCountry(). , builder, .

: US

801 CHESTNUT ST
ST. LOUIS, MO 63101

: JP

〒1600023
NishiShinjuku
3-2-11 NISHISHINJUKU SHINJUKU-KU TOKYO

0

Source: https://habr.com/ru/post/1610033/


All Articles