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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Dealers within a specified radius based on Long and Lat

Status
Not open for further replies.

dodge20

MIS
Jan 15, 2003
1,048
US
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
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 & "&#176; " & 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
 
Let me clarify what I am trying to accomplish as I didn't do a very good job in my first post.

I want a form on my page where I input a zipcode and a radius in miles. I want to query the database and display all dealers located within the specified radius.

I am using asp and mysql database.

Dodge20
 
What type of database are you using. SQL Server, Access, etc...

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I was going to reply earlier, but decided not to. I am not familiar with MySQL. I am, however, VERY familiar with SQL Server. The reason I mention this is because I have a handy dandy little function that can calculate distances in a SQL Server User Defined Function.

So... I would approach this problem by creating a database query to do all the work. I would only use ASP to dipslay the results. In case you are interested, here's a thread that shows how you can calculate distances and then use the calculations to return the correct results from the database.

thread183-1197121

Again, this is for Microsoft SQL Server. I'm not sure how much help this will be for you since you are using MySQL. Sorry.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks for responding gmmastros, I can't seem to make that work, but it may have given me some other ideas. I kind of figured this would be a pretty common thing. I wasn't expecting it to be this difficult when I bought the database.

Dodge20
 
New approach. What about something like this I found on Experts Exchange?

My question, how do I set the session UserLat and UserLon?

Code:
<%
pi = 3.14159265358979323846
UserLat = session("lat")
UserLon = session("long")
distance = request.form("txtdistance")
earth_radius_miles = 6378100 / 0.3048 / 5280
lat_miles_per_deg = earth_radius_miles * pi / 180
lon_miles_per_deg = cos(UserLat) * lat_miles_per_deg

strSQL = "select tblUsers.id, tblUsers.username, tblUsers.Zip, (" & UserLat & "-lat2)*" & lat_miles_per_deg & " AS latdist, (" & UserLon & "-lon2)*" & lon_miles_per_deg & " AS londist, sqrt(latdist*latdist+londist*londist) AS distance from tblUsers inner join tblZips on tblZips = tblUsers.zip 
WHERE lat2 >= " & (UserLat - distance / lat_miles_per_deg) & " AND lat2 <= "& (UserLat + distance / lat_miles_per_deg) & " AND lon2 >= " & (UserLon - distance / lon_miles_per_deg) & " AND lon2 <= " & (UserLon + distance / lon_miles_per_deg) & "
HAVING distance < " & distance & ""

Dodge20
 
Well, the user will be entering their zip code, right? You just need to write a query that will return the lat and lon from the zip_codes table based on the users input.

Be aware that not all 5 digit numbers represent a valid zip code. With 5 digits, there are 100,000 combinations, but only approximately 30 or 40,000 zip codes, so you will need to validate the zip code that the user enters.


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Can you explain how, i don't know how to return a value to a session variable.

Dodge20
 
I guess i should have said I don't know how to return a session variable on the same page. Can this be done on 1 page?

Dodge20
 
Thought about using the Google Maps API? I think there are some pretty good GIS functions you can build.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top