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.