How Can I add Latitude, Longitude dynamically in Javascript?

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

  • 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.

  • Answer:

    Zip Code searching is a widely-solved problem these days. I recently did an implementation in C# based on http://www.codeproject.com/KB/cs/zipcodeutil.aspx, 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. http://www.populardata.com/zip_codes.zip.

HC Foo at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

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.

rokusan

No, adding the two differences is no good. I think you should be using the http://en.wikipedia.org/wiki/Haversine_formula. There's a good Javascript example http://www.movable-type.co.uk/scripts/latlong.html that you can probably convert. One sevice that does free zip to position conversions is http://geocoder.us/, although I haven't used it.

le morte de bea arthur

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 http://en.wikipedia.org/wiki/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.

Project F

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?

zpousman

Please allow people to view a full list of addresses. Sometimes people don't know the ZIP (traveling or looking for someone else).

dripdripdrop

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.

HC Foo

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.