• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question about utilizing array of zip codes

Hi Guys,

I'm working on a function that will find a range of zip codes within a particular passed zip code.

Example

Find all zips within 50 miles of 63101

However, the form allows users to enter in City, State instead of zip.

I've got everything figured out on how to parse out the city and state, find matches in the database, and return all the zips that match for that city.

In the case of say, St. Louis, there are dozens of zip codes. So if someone searches for something within 50 miles of St. Louis, MO, my code, at this point, has a long array of zips that match.

The code I have that searches for other zips within a radius only allows one zip to be passed. If I were to pass all zips, I'd essentially have to loop through each zip, find all zips within a range of each zip in the loop, combine the results, sort out duplicates, etc. etc. This would be very inefficient.

So I'm thinking of the best way to just grab one zip out of the city's array of matching zips and use that in the search.

Ideas.

1. List the array out, and the grab one from the middle. Say the city has 19 zips, grab like the 9th one in the list (sorted numerically) and just use that.

2. Use the first zip in the array, sorted numerically.

3. Use the last zip in the array, sorted numerically.

Thoughts?
 
First, I assume you have a way of finding distances? Assuming the zips are given with lat/long coordinates, something like delta-y=lat1-lat0; delta-x=(long1-long0)*cos((lat0+lat1)/2)

Next, I wouldn't go with any one zip code for the city location. If you have a city location given (see Fuzzy's thread for one way), just use that.

If you're determined to get a location from zip codes, I'd use the centroid of them. But I suspect someone else's definition of the center of a city or town would be better than the Post Office's.
 
Picking an arbitrary zip code could spell trouble. Can you alter the UI so that if it finds multiple zips, it can display all of the found zips and have it prompt the user to narrow it done to one zip?
 
1. I'm trying to make the process as simple as possible, so I'd like to avoid the additional step of selecting a zip code. I was thinking about padding the radius by 10 miles to accommdate for the difference between two zip codes that might be within the same city.

2. Yes, the process of calculating the radius of a particular zip code is already functioning, utilizing lat/long, etc. This part works just fine if I directly feed it a single zip, like 63101. The problem is if someone enters a city, it doesn't just give one zip. So I need to either isolate it to a single zip, or run the range script on every zip, then combine all those results. That's something else I would like to avoid.

3. I was wondering if there was a way to find the most "central" zip of the city, and use that.

4. There is going to be an advanced search option, where users will enter in more specific information, an exact zip code, etc. and "use our advanced search for better results" will be presented as an option. So I'm not to overly concerned about making this uber precise. Just a general search.
 
Houston is over 50 miles from East/West so picking an arbitrary number to pad by is a terrible idea.

As others have said, if you are letting users pick from a list of cities just use the cities location information for the start place instead of an arbitrary zip code.
 
Are you using the array in your language as in a hard coded array? Or will this be calculated then fetched from the database? I'm not aware of CF's abilities but this also sounds like it could be a good candidate for the Google Maps API.
 
Houston is over 50 miles from East/West so picking an arbitrary number to pad by is a terrible idea.

As others have said, if you are letting users pick from a list of cities just use the cities location information for the start place instead of an arbitrary zip code.

Good point.

When I say array, i'm just speaking in a general since. It's not a hard coded, programmatic array.

I could just say List. Or set.. Someone enters St. Louis, and it returns a list of zip codes that are within the city.

Another idea I had, was to take the list of matching zips that are within the city, and utilized the lat/longs of each to calculate a center point, then use that data to find the zip that contains that center point.
 
Good point.

When I say array, i'm just speaking in a general since. It's not a hard coded, programmatic array.

I could just say List. Or set.. Someone enters St. Louis, and it returns a list of zip codes that are within the city.

Another idea I had, was to take the list of matching zips that are within the city, and utilized the lat/longs of each to calculate a center point, then use that data to find the zip that contains that center point.
This, and taking into account Ken_g6's post, I think would be your best bet. Perhaps add up Latitude and divide by the number of zip codes, then add up Longitude and divide by the number as well, and get an average lat/long point. Then from there figure out which in your list is the closest? I'm sure it's not exact but it seems close enough for your application.
 
What's the end use of this? That should really be the prime determinant of how it's implemented. Why are people getting these zip codes?
 
Back
Top