GPS Coordinates And Zip Code

Ivan Luk
3 min readJan 19, 2021

Here in the U.S., regardless of where you live or work, there is no escape from zip codes. Every address in this country has a zip code attached to it. Google Maps is very handy as you can type a zip code in the search box and the map displays where it is. So, what if you want to display a map in your app and center it based on the center of the zip code entered. Google API provides many Maps related functions and here is one can be used.

maps.googleapis.com/maps/api/geocode/json?key=your_API_key&components=postal_code:60610

What you get back in the JSON is a collection of geographic information like county and state but what we are interested in is under “geometry”. The provided result comes back in an array, “results”, and the first element contains the object that provides all the information.

In order to get the GPS coordinates, we need the latitude and longitude. Both can be found respectively under “results[0].geometry.location.lat” and “results[0].geometry.location.lng”. Now that we have obtained the GPS coordinates of the zip code in question, we can now set the “initialCenter” attribute of our map.

Now when the user enters a different zip code, the map will center on that location.

What if I want to do the exact reverse? What if someone gives me a GPS coordinates and I want to find the zip code that coordinates fall within? In my last blog I wrote about the app I wrote to track dog poops in the neighborhood. User can upload a smart phone photo embedded with GPS data and I show the location on a map. In my view, the simplest definition of neighborhood is zip code, without getting into the complexity of town or even subdivision. However, someone can take a picture of a pile of poop outside of his/her zip code while walking around and report the unpicked-up waste. If I need to filter the map display to only show poops in the same zip code as the registered user, I need to know the zip code of where the photo was taken. Fortunately, Google API has that option too.

maps.googleapis.com/maps/api/geocode/json?key=your_API_key&latlng=41.907032, –87.632650

In this case, the returned JSON is a bit more complex. Since the zip code falls under the “address_components”, the “long_name” (or “short_name”) that contains the zip code will not be in the same array position for different zip codes. Google API can return more or less data elements depending on what is available for that zip code. In order to obtain the desired information, we need to focus on the “postal_code” type in the array of objects. This is how I ensure I get the zip code regardless of what Google API returns based on the entered GPS coordinates:

I filter out the element from the “address_components” array that contains the “types” of “postal_code”. Now I can access the “long_name” and obtain the returned zip code.

I hope you find this useful when you play around with GPS coordinates and zip codes in your apps.

--

--

Ivan Luk

Used to manage software development in financial services. Recently acquired skills include full stack development and DNA extraction/sequencing.