Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate distance from zip to zip when searching

Status
Not open for further replies.

snowboardr

Programmer
Feb 22, 2002
1,401
PH
I have a zipcode database and I figured out how to calcuate 2 zipcodes mileage.. however I am not sure how to calculate it when a user searches for another user.. IE:

Show users within

5 miles
10 miles
20 miles
30 miles

etc...

The code to calculate distance using 2 latitudes and longitudes which are included in the zip code db..

is as follows:

Code:
<%
                                                             
const pi = 3.14159265358979323846

Function distance(lat1, lon1, lat2, lon2, unit)
  Dim theta, dist
  theta = lon1 - lon2
  dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta))
  dist = acos(dist)
  dist = rad2deg(dist)
  distance = dist * 60 * 1.1515
  Select Case ucase(unit)
    Case "K"
      distance = distance * 1.609344
    Case "N"
      distance = distance * 0.8684
  End Select
End Function 


'
'  This function get the arccos function from arctan function    
'
Function acos(rad)
  If Abs(rad) <> 1 Then
    acos = pi/2 - Atn(rad / Sqr(1 - rad * rad))
  ElseIf rad = -1 Then
    acos = pi
  End If
End function


'
'  This function converts decimal degrees to radians             
'
Function deg2rad(Deg)
	deg2rad = cdbl(Deg * pi / 180)
End Function

'
'  This function converts radians to decimal degrees             
'
Function rad2deg(Rad)
	rad2deg = cdbl(Rad * 180 / pi)
End Function

%>

The zip code database is set up as follows:

ZIPCODE | LATITUDE | LONGITUDE | CITY | STATE | ABBR

Thanks in advance.

Happy holidays.

Jason


www.sitesd.com
ASP WEB DEVELOPMENT
 
Here is several functions that should provide you with a partial solution:

What you will want to do is to embed these in your SQL calls so they can be calculated on the fly (a stored proc might be better for this) in the WHERE statement (HAVING possibly?) and additionally specify the limit...

-T


barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top