Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

A trick for geolocating US postal codes

It's dumb, but it works

Joel Clermont
Joel Clermont
2024-03-21

From our daily Laravel tips email. Get the next one in your inbox.

On a project, we were using the Google Maps API to translate a US postal code into a latitude and longitude.

I bumped into a problem where a small number of postal codes would return a null result, despite being valid US postal codes.

I have no idea why this was the case. I thought maybe this postal code happened to overlap with a postal code in another country, so it just gave up and returned no results?

It didn't really matter why Google Maps was doing this for a handful of postal codes. I just wanted it to work.

So instead of sending just 12345 to the API, I would append the country code: 12345 US. This worked every time.

Hope this helps,

Joel

P.S. Have a weird API story like this? Hit reply and share!

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.