I have a database with all zipcodes along with their latitude and longitude. I need to find all dealers within a specified radius depending on their zipcode (latitude and longitude).
I found a script on 4guysfromrolla.com, but I am not sure how to implement it with my database.
2 Tables
zip_codes
city
state
zip
lat
long
Dealer
name
address
city
state
zip
Here is the code I found
In a later article they say thing
Now you can create a SQL statement which limits the recordset of cities in this manner:
Can someone help me implement this?
Thanks
Dodge20
I found a script on 4guysfromrolla.com, but I am not sure how to implement it with my database.
2 Tables
zip_codes
city
state
zip
lat
long
Dealer
name
address
city
state
zip
Here is the code I found
Code:
<%
option explicit
const pi = 3.14159265358979323846
function GetDistance(Lat1, Long1, Lat2, Long2, Unit) dim x
' do the nasty calcs
x = (sin(DegToRads(Lat1)) * sin(DegToRads(Lat2)) + cos(DegToRads(Lat1)) * _
cos(DegToRads(Lat2)) * cos(abs((DegToRads(long2))-(DegToRads(long1)))))
' Get Acos(x)
x = atn((sqr(1-x^2))/x)
' Get distance in kilometers
GetDistance = 1.852 * 60.0 * ((x/pi)*180)
' Convert units if necessary
select case ucase(Unit)
case "M"
GetDistance = GetDistance / 1.609344
case "N"
GetDistance = GetDistance / 1.852
end select
end function
function DegToRads(Deg)
DegToRads = cdbl(Deg * pi / 180)
end function
sub DecimalDegToDMS(DecimalDegrees, Degs, Mins, Secs)
Dim temp
Degs = fix(DecimalDegrees)
temp = (DecimalDegrees - Degs) * 60.
Mins = fix(temp)
temp = (temp - Mins) * 60.
Secs = fix(temp)
end sub
dim d, m, s
response.write "<html><body>Calculating the distance between Dallas (75248) and San Antonio (78201)<br><br>"
response.write GetDistance(32.9697, -96.80322, 29.46786, -98.53506, "M") & " Miles<br>"
response.write GetDistance(32.9697, -96.80322, 29.46786, -98.53506, "K") & " Km<br>"
response.write GetDistance(32.9697, -96.80322, 29.46786, -98.53506, "N") & " Nautical Miles<br>"
DecimalDegToDMS 32.9697, d, m, s
response.write "Dallas' decimal latitude of 32.9697 can also be shown as " & d & "° " & m & "' " & s & """"
response.write "</body></html>"
response.end
%>
In a later article they say thing
Code:
' THIS VARIABLE SETS THE RADIUS IN MILES
iRadius = 150
LatRange = iradius / ((6076 / 5280) * 60)
LongRange = iRadius / (((cos(cdbl(iStartLat * _
3.141592653589 / 180)) * 6076.) / 5280.) * 60)
LowLatitude = istartlat - LatRange
HighLatitude = istartlat + LatRange
LowLongitude = istartlong - LongRange
HighLongitude = istartlong + LongRange
Now you can create a SQL statement which limits the recordset of cities in this manner:
Code:
SELECT *
FROM Locations
WHERE Latitude <= [HighLatitude]
AND Latitude >= [LowLatitude]
AND Longitude >= [LowLongitude]
AND Longitude <= [HighLongitude]
Can someone help me implement this?
Thanks
Dodge20