5

Similar to this question from 2011, which lacks a satisfactory answer:

An app that I'm working on will be deployed internationally. The app itself only cares about the lat/long of an Address, but it will need to display the Address to the user in a multi-line format. Google's geocoder provides a formatted address, but it's on a single line separated by commas. Splitting this into lines would require knowledge of how multiline addresses are formatted in a given country. In the US, for example, it's customary to place the city and state on the same line, separated by a comma.

Is there a built-in method (or third-party library or Web service) that will format a multi-line address from an Address, taking into account that reverse-geocoded addresses may be incomplete?

Community
  • 1
  • 1
Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82

1 Answers1

0

Check out googlei18n/libaddressinput: Google’s postal address library, powering Android and Chromium. There are two modules in the project :android and :common. You should only need :common 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 this is where the format interpreter pulls the address format from. (See RegionDataConstants for the list of formats, if you're curious.)

Sets the 2-letter CLDR region code of the address; see AddressData#getPostalCountry(). Unlike other values passed to the builder, the region code can never be null.

Example: US

801 CHESTNUT ST
ST. LOUIS, MO 63101

Example: JP

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

Funktional
  • 4,083
  • 1
  • 30
  • 27
  • 1
    This library looks very interesting, but the documentation doesn't say how I can install it in my project's build.gradle, where the library is available and so on. @Funktional can you please elaborate on that? – Sebastien Feb 22 '18 at 19:36
  • @Sebastien Personally- I installed the project as a git submodule and then simply imported the common library using: compile project(':libaddressinput') – Funktional Feb 27 '18 at 18:43