我如何才能将直接从 Google 地点 API 获得的地址组件明智地分叉?

How can I bifurcated address component wise which I am getting directly from Google Place API?

我想将城市、州、国家/地区固定到不同的变量中,就像我获取地址、纬度和经度一样。但我不知道如何获取地址组件。

public class MainActivity extends AppCompatActivity {
String TAG = "placeautocomplete";
String API = "xxxxxxxxxxxxxxxxx";
String Latitude;
String Longitude;
double lat, lng;
String Address,Place_name,Phone,Complete_address;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edit = findViewById(R.id.editText);
    txtView = findViewById(R.id.txtView);
    // Initialize Places.
    Places.initialize(getApplicationContext(), API);
    // Create a new Places client instance.
    PlacesClient placesClient = Places.createClient(this);

    // Initialize the AutocompleteSupportFragment.
    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), API);


    }

    // Initialize the AutocompleteSupportFragment.
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

    assert autocompleteFragment != null;

    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.LAT_LNG, Place.Field.ADDRESS,Place.Field.PHONE_NUMBER));
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());

            if (place.getLatLng() !=null){
                lat =place.getLatLng().latitude;
                lng =place.getLatLng().longitude;

            }
            Latitude = String.valueOf(lat);
            Longitude= String.valueOf(lng);
            Address= place.getAddress();
            Place_name = place.getName();
            Phone = place.getPhoneNumber();

        }
        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i(TAG, "An error occurred: " + status);
        }
    });
}
 }

我只想在 Address、City、State、Pin 等组件中添加地址。

你不应该使用 Address= place.getAddress() 而应该使用 .getAddressComponents(),因为 .getAddress() returns String 具有人类可读的地点地址和官方文档 Google 写道:

Do not parse the formatted address programmatically. Instead you should use the individual address components, which the API response includes in addition to the formatted address field.

所以,你应该使用Place.getAddressComponents() to get List<AddressComponent> and than get name and type of each address component. Or use additional request for place lat/lng and Geocoder.getFromLocation() like in Display a location address官方例子:

...
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;

...
addresses = geocoder.getFromLocation(
            Latitude,    // <-- your Latitude = String.valueOf(lat);
            Longitude,   // <- your Longitude= String.valueOf(lng);
            location.getLongitude(),
            // In this sample, get just a single address.
            1);

// Handle case where no address was found.
if (addresses == null || addresses.size()  == 0) {
    if (errorMessage.isEmpty()) {
        errorMessage = getString(R.string.no_address_found);
        Log.e(TAG, errorMessage);
    }
    deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
    Address address = addresses.get(0);
    ArrayList<String> addressFragments = new ArrayList<String>();

    // Fetch the address lines using getAddressLine,
    // join them, and send them to the thread.
    for(int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
        addressFragments.add(address.getAddressLine(i));
    }
}
...

也看看Developer Guide

P.S。不要对变量使用大写名称(例如,Latitude)——这是 类 的风格。将其命名为 latitude.