What are some best practices for creating an online office locator?
June 23, 2008 9:54 AM   Subscribe

What are some best practices for creating an online office locator?

We have multiple offices around our state, and I'm creating an office locator tool that will help people find the nearest office when they enter a zip code. I'm having a hard time wrapping my brain around the logic I need to use. Here is what I have come up with:

1-Calculate the Longitude and Latitude of all of our offices, and store it in a database.
2-Calculate the longitude and latitude of the center of the zip code entered.
3-Subtract the longitude of the zip from the longitude of the offices.
4-Subtract the difference between the latitude of the zip from the latitude of the offices.
5-Add the 2 differences together, an organize them in an ascending order.
6-Output the list of offices with the closest one to the top.

Is this the best logic for an office locator? Can I get the longitudes and latitudes from something online like the Google Maps API?

Any other suggestions would be very helpful.
posted by HC Foo to Computers & Internet (7 answers total) 6 users marked this as a favorite
No, adding the two differences is no good. I think you should be using the Haversine formula. There's a good Javascript example here that you can probably convert.

One sevice that does free zip to position conversions is geocoder.us, although I haven't used it.
posted by le morte de bea arthur at 10:02 AM on June 23, 2008

Best answer: Zip Code searching is a widely-solved problem these days. I recently did an implementation in C# based on this code, and I know Drupal offers similar functionality, so there's definite PHP code out there as well. If you can't find pre-rolled code in your language, it shouldn't be hard to translate if you know a little math.

Zip Code DB's are widely available as well. Here's one.
posted by mkultra at 10:03 AM on June 23, 2008

Best answer: You can also just match the first five, failing to four, failing to three, failing to two digits of the ZIP code. Faster and less prone to the fuzzy weirdnesses of maps.

People prefer a choice, anyway. The "closest" by formula is often not the one that a user FEELs is closest, or that they'd prefer, since "Oh look that one's near Rob's house" and "that one's right on my way to work" can't happen if you're too dictatorial with a "find closest" approach.

Be fuzzy, show many options, let users decide which is really closest and most convenient to them. It's more human-friendly.
posted by rokusan at 10:08 AM on June 23, 2008 [1 favorite]

You can get lat/long out of google using their geocoding API, but be warned, it sometimes gives insane results. You might do better to grab the center of each zip code from somewhere more reliable, and then if you want to do address level location, do the google geocoding, but throw away the result if it's more than N miles from the known center of the zip.

(Seriously. Google likes to tell you that your address is in some different continent.)

For the actual sorting, you don't add the distances, you use Great Circle Distance. If you don't understand that page, look for great circle distance, haversine formula, or earth distance and your favorite programming language. It's a well-solved problem, so you shouldn't have to trouble yourself if you don't understand the math.
posted by Project F at 10:16 AM on June 23, 2008

If you're in a small state, the great-circle distance is all but equal to the 2D distance. Just saying. Unless a user of your website really cares whether the office in Istambul is (a bit) closer than the office in Caracas, who cares?
posted by zpousman at 11:21 AM on June 23, 2008

Please allow people to view a full list of addresses. Sometimes people don't know the ZIP (traveling or looking for someone else).
posted by dripdripdrop at 4:25 PM on June 23, 2008

Response by poster: Thanks for the feedback. I am planning on having a zip code search, and a drop down list with all of our locations. They will have multiple ways to search for offices. The search results will also show multiple locations, so they can choose which one "feels" closest.
posted by HC Foo at 7:21 AM on June 24, 2008

« Older How should I tote my stuff, including a laptop, on...   |   Need to identify Hot Wheels part Newer »
This thread is closed to new comments.