SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jun 2004
    Location
    Montreal
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Zip Code Search With Distance

    I am currently buidling a mini-application (php and mysql) of store locations that are in the united states. People that will use this search tool will be searching by zip code.

    The idea is not to have a "no results found" but rather give a result of the nearest store to the entered zip code.

    The way i thought to do this was to use a webservice to calculatte the distances between zip codes.

    Can anyone recommend which webservice would be compatible for this or suggest another solution?

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,423
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    You need to purchase a database that contains GPS info for each zip code. Then your query needs to do math to find other zipcodes at the specified distance by doing math based longitude and latitude of each zipcode.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can do this yourself.

    get a zipcode database, probably $50 or so for a good up to date one that includes latitude and longitude(i havent checked prices for a while). you might be able to find a free one, although it may not be as accurate or up to date.

    then its a matter of math.

    http://www.google.com/search?hl=en&q...=Google+Search

    in your case i think you would:
    grab the users starting zipcode, query db to get lat/long
    then query your db to compare and calculate distance for the lat/long of each of your stores. order results by distance.


    i know some of the companys that sell these databases also offer a webservice. only problem with this is its going to be slower, and your code will fail if you cannot contact thier webservice due to network issues.

  4. #4
    SitePoint Zealot fatnic's Avatar
    Join Date
    Aug 2006
    Location
    Lincoln, UK
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've done something quite similar and it's pretty easy to do. And I'm a complete newbie! You'll need this function...

    PHP Code:
    function distance($lat1$lon1$lat2$lon2$unit) { 

      
    $theta $lon1 $lon2
      
    $dist sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
      
    $dist acos($dist); 
      
    $dist rad2deg($dist); 
      
    $miles $dist 60 1.1515;
      
    $unit strtoupper($unit);

      if (
    $unit == "K") {
        return (
    $miles 1.609344); 
      } else if (
    $unit == "N") {
          return (
    $miles 0.8684);

        } else {
            return 
    $miles;
          }

    The way I did it was to have a CSV file of all the postcodes (British) and their long, lats. Then I wrote some code to check the distance between two postcodes submitted in a form. Not exactly what you are after but it will hopefully be a start. Excuse my code. Like I said, i'm a newbie!

    PHP Code:
    $pcode1 $_GET['pcode1'];
    $pcode2 $_GET['pcode2'];

    $file fopen('postcodes.csv''r');

    while ((
    $psearch fgetcsv($file 10000",")) !== FALSE)
    {
        if (
    $psearch[0] == $pcode1)
        {
            
    $p1long $psearch[4];
            
    $p1lat $psearch[5];
        } 
        
        if (
    $psearch[0] == $pcode2)
        {
            
    $p2long $psearch[4];
            
    $p2lat $psearch[5];
        } 
    }

    fclose($file);

    echo 
    "Distance from ".$pcode1." to ".$pcode2." is ";
    printf "%.2f"distance($p1long$p1lat$p2long$p2lat"M"));
    echo 
    " miles.<br>"
    Of course the hard part will probably be finding the zipcode CSV file.

    Hope this helps.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You could probably use a thrid-party like Google Maps, however i don't know of all what you can do with there API.

    http://www.google.com/apis/maps/
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Enthusiast Everah's Avatar
    Join Date
    Mar 2004
    Location
    East Bay Area, CA, USA
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This site has the table data for free. If you checkout PHP Classes there are a few different zip locator classes out there.
    Me | My Family | My Design Firm
    Much of what I say can be refuted or disputed.
    I am just a lowly professional PHP developer.
    I could be just as wrong as the next guy.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •