# Autocomplete Error: USA Country Filter Returns Mexican Cities
## Summary
- When users selected "United States" in a country-autocomplete field, the dependent city-autocomplete field incorrectly displayed Mexican cities
- The app used Google Maps Legacy Place Autocomplete with country restriction parameters (`AutocompleteFilter`)
- All tested countries (UK, France, Spain, Russia) worked correctly – error occurred only with USA selection
- Implementation followed official Google Places SDK documentation
## Root Cause
- **Incorrect country code handling**: The autocomplete filter used `country("US")` which includes:
- US territories (Puerto Rico, Guam)
- Bordering cities under US postal service conventions
- Mexican cities near the border due to common NAFTA address formatting
- **Geopolitical boundary ambiguity**: Google's Places API treats border regions as overlapping zones for autocomplete results
- **US-specific handling**: Unlike other countries, the US filter includes neighboring areas due to historical addressing practices
## Why This Happens in Real Systems
- Legacy Autocomplete APIs lack granular border control for closely integrated regions
- US/Mexico cross-border commerce results in shared location metadata in Google's index
- No distinction between "political boundaries" vs "service coverage areas" in Place Autocomplete:
- The API prioritizes user experience (returning potentially relevant results) over strict geopolitical boundaries
- Testing gaps: Developers often test obvious countries but miss edge cases like border regions
## Real-World Impact
- Users in southern US states see irrelevant Mexican city suggestions
- Address validation fails when Mexican cities appear as "US locations"
- Potential data privacy concerns when storing incorrectly categorized user locations
- User frustration and perceived application unreliability
- Business impact: Invalid location data could skew analytics or service coverage reports
## Example or Code
**Original Implementation:**
```java
// Country filter setup
AutocompleteFilter countryFilter = new AutocompleteFilter.Builder()
.setCountry("US") // Problematic for USA
.build();
// City filter (dependent on country selection)
AutocompleteFilter cityFilter = new AutocompleteFilter.Builder()
.setCountry(selectedCountryCode)
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
.build();
How Senior Engineers Fix It
-
Add secondary validation layer:
if (place.getCountryCode().equals("US") && !isValidUSCity(place.getName())) { // Reject or flag mismatches } -
Implement geo-coordinate verification:
Geocoder geocoder = new Geocoder(context); List<Address> addresses = geocoder.getFromLocation( place.getLatLng().latitude, place.getLatLng().longitude, 1 ); if (addresses.size() > 0 && !"US".equals(addresses.get(0).getCountryCode())) { // Handle non-US result } -
TypeFilter combination:
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS) .setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS) -
Migrate to new Places SDK: Newer APIs have improved border handling
-
Client-side filtering: Post-process results to remove cities with non-US country codes
Why Juniors Miss It
- Assumption that
setCountry()uses exact political boundaries - Over-reliance on Google’s documentation without testing border edge cases
- Testing only with European countries (which have clearer borders)
- Not accounting for US-specific regional complexities
- Lack of defensive programming for geo-political edge cases
- Misinterpreting autocomplete “cities” filter as strictly administrative boundaries