如何通过PlaceAutocomplete API获取城市与州名称?附现有代码求助
Got it, let's adjust your code to pull the state (administrative area level 1) along with the city using the PlaceAutocomplete API. Here's how you can do it step by step:
1. Keep Your Existing Autocomplete Filter & Intent Setup
Your current filter targeting Indian regions works perfectly—we just need to add logic to extract the state once a place is selected. Here's your original code (cleaned up for clarity):
// Set up the filter to target regions in India AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder() .setCountry("IN") .setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS) .build(); // Launch the PlaceAutocomplete activity Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN) .setFilter(autocompleteFilter) .build(); startActivityForResult(intent, YOUR_REQUEST_CODE); // Replace YOUR_REQUEST_CODE with your defined integer
2. Handle the Result to Extract City & State
The magic happens when you parse the selected place's address components in onActivityResult. The state corresponds to the TYPE_ADMINISTRATIVE_AREA_LEVEL_1 component, while the city is usually TYPE_LOCALITY (adjust to TYPE_SUBLOCALITY if you need a smaller geographic division).
Here's the code to handle the result:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == YOUR_REQUEST_CODE && resultCode == RESULT_OK) { // Get the selected place from the intent Place selectedPlace = PlaceAutocomplete.getPlace(this, data); // Variables to store our target values String cityName = null; String stateName = null; // Loop through address components to pick out city and state for (AddressComponent component : selectedPlace.getAddressComponents().asList()) { List<String> componentTypes = component.getTypes(); // Grab city name (use TYPE_SUBLOCALITY if you need a neighborhood-level name) if (componentTypes.contains(AddressComponent.TYPE_LOCALITY)) { cityName = component.getLongName(); } // Grab state name (administrative area level 1 is the standard for states) if (componentTypes.contains(AddressComponent.TYPE_ADMINISTRATIVE_AREA_LEVEL_1)) { stateName = component.getLongName(); } } // Use the retrieved values as needed if (cityName != null && stateName != null) { Log.d("PlaceData", "City: " + cityName + ", State: " + stateName); // Update your UI or process the data here } } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { // Handle error cases (e.g., API key issues, network errors) Status errorStatus = PlaceAutocomplete.getStatus(this, data); Log.e("PlaceError", errorStatus.getStatusMessage()); } }
Quick Tips:
- If you prefer abbreviated state names (like "MH" for Maharashtra), swap
component.getLongName()withcomponent.getShortName(). - Double-check your manifest has the required Places API permissions and your API key is correctly configured—this is a common pitfall!
- Test with a few different locations to make sure you're grabbing the right component types (some regions might label cities differently).
内容的提问来源于stack exchange,提问作者Honeywell

