# Given coordinates, how do I get all the Zip Codes within a 10 mile radius?

I have a location (latitude & longitude). How can I get a list of zipcodes that are either partially or fully within the 10 mile radius of my location?

The solution could be a call to a well known web service (google maps, bing maps, etc...) or a local database solution (the client has sql server 2005) or an algorithm.

I have seen the somewhat similar question, but all the answers there pretty much pertain to using SQL Server 2008 geography functionality which is unavailable to me.

• What country are you trying to find zip codes for? Nov 16, 2010 at 2:06

Start with a zip code database that contains zipcodes and their corresponding latitude and longitude coordinates:

To get the distance between latitude and longitude, you will need a good distance formula. This site has a couple variations:

http://www.meridianworlddata.com/distance-calculation/

The "Great Circle Distance" formula is a little extreme. This one works well enough from my experience:

``````sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1)
and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)
``````

Your SQL Query will then look something like this:

``````select zd.ZipCode
from ZipData zd
where
sqrt(
square(69.1 * (zd.Latitude - @Latitude)) +
square(69.1 * (zd.Longitude - @Longitude) * cos(@Latitude/57.3))
) < @Distance
``````

Good luck!

• This was actually a great answer... all the distances are within a quarter mile that I have figured... +1 Apr 27, 2012 at 19:13
• great answer +1 - can you tell me where the maths comes from? What do the decimal numbers represent? Mar 13, 2014 at 13:22
• The link to meridianworld.com is no longer valid, so I updated it to a cached version. I believe the magic numbers come from some distance approximation that calculates distances on the surface of a sphere. There are more accurate formulas out there, but for your basic "store locator" scenario, this one has worked well for me.
– dana
Mar 13, 2014 at 15:56
• @dana it looks like the link still works if you drop the .asp extension (meridianworlddata.com/distance-calculation) Jul 12, 2016 at 21:21

Firstly, you'll need a database of all zipcodes and their corresponding latitudes and longitudes. In Australia, there are only a few thousand of these (and the information is easily available), however I assume it's probably a more difficult task in the US.

Secondly, given you know where you are, and you know the radius you are looking for, you can look up all zipcodes that fall within that radius. Something simple written in PHP would be as follows: (apologies it's not in C#)

``````function distanceFromTo(\$latitude1,\$longitude1,\$latitude2,\$longitude2,\$km){
\$delta_latitude  = \$latitude2  - \$latitude1;
\$delta_longitude = \$longitude2 - \$longitude1;
\$temp = pow(sin(\$delta_latitude/2.0),2) + cos(\$latitude1) * cos(\$latitude2) * pow(sin(\$delta_longitude/2.0),2);
{"ft_lang_label":"__label__en","ft_lang_prob":0.7024206,"math_prob":0.884814,"size":1049,"snap":"2022-40-2023-06","text_gpt3_token_len":284,"char_repetition_ratio":0.19617225,"word_repetition_ratio":0.0,"special_character_ratio":0.29361296,"punctuation_ratio":0.17714286,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9773127,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-30T00:06:25Z\",\"WARC-Record-ID\":\"<urn:uuid:e231cd00-1f54-43b4-9e46-b722a58b9c3c>\",\"Content-Length\":\"173930\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0dbad74b-fb81-41bd-8dd9-d21717d23f04>\",\"WARC-Concurrent-To\":\"<urn:uuid:22de61d8-aff2-4212-ae65-cdcd9c0ebfe8>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/4190154/given-coordinates-how-do-i-get-all-the-zip-codes-within-a-10-mile-radius\",\"WARC-Payload-Digest\":\"sha1:6TZ75AXTRQROYHAUNHHK5ZWFTZEBVPIG\",\"WARC-Block-Digest\":\"sha1:QKXAB2ASQRWDI6WB4VAWJE6PD7UZ5V63\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335396.92_warc_CC-MAIN-20220929225326-20220930015326-00040.warc.gz\"}"}