2

I've seen all sorts of "store locator"-type solutions on the Web for finding, for example, stores within a given radius of a particular Zip Code, and they have fancy maps that display the nearest stores, etc. Many use Google Maps or other databases, but many also rely on exact latitude and longitude conversions, etc. But that's all overkill for my project.

In my case, I'm just processing orders on the back end (using PHP) and I already have a list of all the ZIP Codes of all of my vendors' warehouses.

I simply want my customer to provide their Zip Code and with a single call I'd like to get back a sorted list of the warehouses nearest to my customer. Zip Codes are good enough - I don't wanna bother with latitude and longitude.

Any ideas?

ColdCold
  • 4,181
  • 2
  • 26
  • 20
  • You will have to associate latitudes and longitudes with zip codes. Zip codes alone do not have enough geographical information encoded in them to calculate (accurately) proximity. (Unless there's something I'm missing in the zip code format... reading the wikipedia article now) – Corbin Dec 18 '11 at 05:07

1 Answers1

5

You should download Popular Data's ZIP code database.

With this, you can use the following to calculate distance.

function calc_distance($point1, $point2)
{
    $radius      = 3958;      // Earth's radius (miles)
    $deg_per_rad = 57.29578;  // Number of degrees/radian (for conversion)

    $distance = ($radius * pi() * sqrt(
                ($point1['lat'] - $point2['lat'])
                * ($point1['lat'] - $point2['lat'])
                + cos($point1['lat'] / $deg_per_rad)  // Convert these to
                * cos($point2['lat'] / $deg_per_rad)  // radians for cos()
                * ($point1['long'] - $point2['long'])
                * ($point1['long'] - $point2['long'])
        ) / 180);

    return $distance;  // Returned using the units used for $radius.
}

Above was stolen from Adam Bellaire's answer in Calculating distance between zip codes in PHP. After doing this calculation, you can simply sort the results.

Community
  • 1
  • 1
kba
  • 19,333
  • 5
  • 62
  • 89
  • That just addresses numerical order, which will, with fairly good frequency, correspond well to distance ordering. However, this has some serious limitations. The main one that comes to mind is state borders (or maybe region borders, depending on how zip codes are laid out). In other words: this is not a solution unless his problem is very specific. Also, that global could be avoided with either a closure a simple little class. – Corbin Dec 18 '11 at 05:14
  • Right, which is the wrong approach. He's trying to over simplify it. He can do it with just zip codes (and without lots of little business rules, your approach is likely the best). In that respect, your answer is a good one. As I said though, it neglects to even mention that it's limited in its accuracy. – Corbin Dec 18 '11 at 05:17
  • It's quite reasonable if the nearest-warehouse thing is just a heuristic anyway and nothing really bad happens if something ships from the second-nearest or third-nearest warehouse. – Gravity Dec 18 '11 at 05:51
  • @Cobin I've completely rewritten the answer by the way. – kba Dec 18 '11 at 05:54