|
Overview
The problem with most "Nearest Dealer Locators" is that they are not efficient. The USA has just under 42,000 ZIP Codes, while Canada has a whopping 770,000+ postal codes. Imagine if, every time you wanted to find all ZIP Codes within 100 miles of any city, your server was required to calculate the distance between the city and all 42,000 ZIP Codes. It would take quite a while - and let's not even begin to think about Canada!
The Solution
So, how do you solve it? The genius is in the solution. What if you could draw a horizontal line on a map 100 miles north and 100 miles south of the city as well as a line 100 miles east & west of the same city? Notice the red square on the map? (see map link below) That's what you'd get.
Now you can easily find all ZIP Codes in that range using an efficient database query - simply find all records where the ZIP Code's latitude & longitude coordinates fall within that red square (see map).
A sample database query would look like this: SELECT * FROM ZIPCodes WHERE Latitude > BottomLine AND Latitude < TopLine AND Longitude > LeftLine AND Longitude < RightLine where TopLine, BottomLine, LeftLine, and RightLine are the appropriate lat or long coordinate.
Database Experts Note: The above query is where you could perform an INNER JOIN of your dealer list with the ZIP Code database. This will, in most cases, drastically reduce the number of records returned and thus make the entire process more efficient.
Finding the Exact Distance
Once you've retreived all ZIP Code records (with corresponding lat/long points), you can then run each record through the ZIP Code Distance Assistant to find the exact distance and see if it fall on the inside or outside of the blue circle on the map. And that's it! You've successfully (and efficiently) found all records within 100 miles of the city!
Summary & Review
To find all "dealers" - or simply postal codes within X miles of a city:
- Get the latitude & longitude coordinate (Point A) of a city
- Find the lines of latitude & longitude to the north, south, east and
west of Point A (My City)
- Perform a database query which returns all records with the lines of
lat/long to each side of Point A
- If needed, make the above query an INNER JOIN with your dealer database
table.
- Loop through each returned record and calculate the distance between Point A
and each record
- Records more than X miles are disposed of, and records less than or equal to
X miles are included in a list
- Display results list to the user
|