Find all Zipcodes at a given distance from zipcode

I know this question sounds like a repetition of several questions here, but none of these questions answered what I wanted. I am interested to know if anyone knows how to find other zipcodes within the radius of the given zipcode. I have a zipcode database with me with latitudes n longitudes, but I'm not sure how to do this in VB.net.

for example, 90069 is a zipcode, and if someone says that the radius is 5 miles, I want all zipcodes, such as 90210,90045,90034, etc., to appear.

Thank you for accepting code samples.

EDIT: I have a MySQL database.

0
source share
2 answers

Here is something that I wrote a long time ago that can start you in the right direction.

While you requested VB.Net, you really need a query that the Great Circle executes to determine the distance between the two points indicated by latitude and longitude.

So, making the following assumptions:

  • Your zip code information is in the same table.
  • The specified table has attributes for lat and lon, which are the approximate centroid of the zip code.

You can use the LINQ to SQL query, which creates the desired result set using something like this

Const EARTH_RADIUS As Single = 3956.0883313286095 Dim radCvtFactor As Single = Math.PI / 180 Dim zipCodeRadius As Integer = <Your Radius Value> Dim zipQry = From zc In db.ZipCodes Where zc.Zip = "<Your Zip Code>" _ Select zc.Latitude, zc.Longitude, ZipLatRads = RadCvtFactor * zc.Latitude, ZipLonRads = RadCvtFactor * zc.Longitude Dim zipRslt = zipQry.SingleOrDefault() If zipRslt IsNot Nothing Then Dim zcQry = From zc In db.ZipCodes _ Where zc.Latitude >= (zipRslt.Latitude - 0.5) And zc.Latitude <= (zipRslt.Latitude + 0.5) _ And zc.Longitude >= (zipRslt.Longitude - 0.5) And (zc.Longitude <= zipRslt.Longitude + 0.5) _ And Math.Abs(EARTH_RADIUS * (2 * Math.Atan2(Math.Sqrt(Math.Pow(Math.Sin(((RadCvtFactor * zc.Latitude) - zipRslt.ZipLatRads) / 2), 2) + _ Math.Cos(zipRslt.ZipLatRads) * Math.Cos(RadCvtFactor * zc.Latitude) * _ Math.Pow(Math.Sin(((RadCvtFactor * zc.Longitude) - zipRslt.ZipLonRads) / 2), 2)), _ Math.Sqrt(1 - Math.Pow(Math.Sin(((RadCvtFactor * zc.Latitude) - zipRslt.ZipLatRads) / 2), 2) + _ Math.Cos(zipRslt.ZipLatRads) * Math.Cos(RadCvtFactor * zc.Latitude) * _ Math.Pow(Math.Sin((RadCvtFactor * zc.Longitude) / 2), 2))))) <= zipCodeRadius _ Select zc End If 

It looks complicated because it is. Here are much smarter people who can explain the algorithm. I just implemented this from some SQL code that I found on the Internet - I can't remember where it came from. A Google search should find you there.

The first query (zipQry) returns the lat and lon of the initial zip code in both degrees and radians. These results are then used to execute the second query.

The first part of the WHERE clause in the second query:

 Where zc.Latitude >= (zipRslt.Latitude - 0.5) And zc.Latitude <= (zipRslt.Latitude + 0.5) _ And zc.Longitude >= (zipRslt.Longitude - 0.5) And (zc.Longitude <= zipRslt.Longitude + 0.5) _ 

It just narrowed the list of zip codes to be checked, making the request work much faster. It adds an arbitrary amount in lat and lon, so you do not check all zipcodes in Ohio when looking for a radius in California. The rest is part of the aforementioned Great Circle Distance algorithm.

Perhaps this was done in one request for greater efficiency, but at that time I needed it, the reasons that are now lost for me.

+3
source

Here is a simple function to call - http://www.zipcodeapi.com/API#radius . The call looks like this: http://www.zipcodeapi.com/rest/ /radius.///.

This ZipcodeAPI service is free up to 50 requests per hour and only $ 250 per year for 5000 requests per hour.

0
source

All Articles