Prompt
How do I troubleshoot reverse geocoding returning the wrong city?
Latest observation
When reverse geocoding returns the wrong city, the issue is usually one of these: the coordinate is slightly off, the geocoder’s data is incomplete/outdated, or you’re reading the wrong field from the response. Here’s a practical troubleshooting checklist.
1) Verify the input coordinates
- Make sure latitude/longitude are in the correct order.
- Common bug: swapping
lat, lngvslng, lat.
- Common bug: swapping
- Confirm the coordinates are in the expected region.
- If the point is near a city border, the “wrong” city may actually be the nearest mapped locality.
- Check for low-precision coordinates.
- Rounded coordinates can move a point across a boundary.
2) Inspect the full geocoder response
Don’t just read one label. Look at:
formatted_addressaddress_componentstypes- administrative levels:
- city / locality
- county
- state / province
- neighborhood / sublocality
Sometimes the geocoder returns:
- a neighborhood instead of a city,
- a postal town instead of a municipality,
- or the county when no city is available.
3) Make sure you’re extracting the right component
Different providers use different names for “city”:
- Google:
locality, sometimespostal_town,administrative_area_level_3 - Mapbox:
place,district,locality - Nominatim/OpenStreetMap:
city,town,village,municipality
If your code only checks locality, you may miss the correct city-like field.
4) Handle border and edge cases
Reverse geocoding can legitimately return an adjacent city if:
- the point is on or near a boundary,
- the boundary data is simplified,
- the “city center” label is used instead of the legal boundary.
If you need the legally correct municipality, use boundary polygons or a GIS lookup instead of plain reverse geocoding.
5) Check geocoder provider/data freshness
- Maps data can be outdated or inconsistent across providers.
- One provider may classify places differently from another.
- Try the same coordinates with a second geocoder to compare results.
6) Look for snapping or map-matching issues
If the coordinates come from GPS or a routing API:
- they may have been snapped to a road,
- or adjusted to a nearby path/road segment, which can move them into another city.
7) Confirm country/region bias settings
Some geocoders use hints that affect results:
languageregioncountry- proximity bias
A bias can make the geocoder favor a nearby city with the same name.
8) Test with known points
Create a small test set:
- downtown points,
- border points,
- points near suburb boundaries,
- points in rural areas.
Compare expected vs returned city and identify patterns.
9) If using cached results, invalidate them
You might be seeing stale cached geocodes.
- Check cache keys include full precision coordinates.
- Ensure cache TTL is reasonable.
- Clear cache and retest.
10) Decide what “wrong city” means in your app
There are multiple valid interpretations:
- nearest populated place,
- postal city,
- administrative city,
- city boundary containing the point.
If your app needs one specific definition, you may need to:
- prefer a specific address component,
- combine reverse geocoding with boundary data,
- or use a municipal polygon dataset.
Quick debugging steps
- Print the raw reverse-geocode response.
- Verify lat/lng order.
- Compare against a second geocoder.
- Test a point well inside the city, not near a border.
- Check whether you need
locality,town,postal_town, or polygon-based lookup.
If you want, I can help you debug a specific reverse geocoding response or code snippet.