Jump to content

Zip Code Search


Liam
 Share

Recommended Posts

Hello all,

Hopefully this is the right place for this question…

I am going to be working on a website that will need a dealer locator. Basically what I need is a search by zip code that will spit out the closest dealers address' to the users search query.

Can anyone give me any pointers on how this might be accomplished?

Thank in advance for any help that can be provided.

Link to comment
Share on other sites

hi liam, welcome to the forum!

you need zip with lat/lng in your database and then you can do something like this:

https://www.dougv.com/2009/03/getting-all-zip-codes-in-a-given-radius-from-a-known-point-zip-code-via-php-and-mysql/

http://stackoverflow.com/questions/8549940/zip-code-proximity-calculation-in-php

maybe you have to keep an eye on performance depending on your dataset...

  • Like 1
Link to comment
Share on other sites

Thank you BernhardB,

The information you provided is very helpful indeed. Now my question is how I can get this implemented into processwire. We will have individual dealer pages that will need to be listed that fall into the zip codes search radius.

I guess I need to know how to connect the zip code field on the dealer page to the zip code database... hope this makes sense.

Link to comment
Share on other sites

you could for example create a treenode for all zip-codes with lat and lng values:

- zipcodes
  |- 100 (title = 100, lat = 16..., lng = 48...)
  |- 101 (title = 101, lat = 15..., lng = 47...)

then calculate the distance like showed in my links, but you dont need to query the database when you are using processwire pages. its something like:

<?php
$zip = ... // center of search
$radius = ... // radius to search
$results = ... // array to store results
foreach($pages->get('/zipcodes')->children as $item) {
    // calculate distance to requested center
    $distance = ... // see stackoverflow example
    if($distance <= $radius) {
        $results[] = array($item->id, $distance);
    }
}

// order $results by distance
// render each result

sorry, 1 step missing. after you have your zip-codes within your radius you would have to get all pages that match that zip-code. you could use a pagefield in your dealer's page to choose the zip-code (it has to reference the pages under /zipcodes so that you can calculate the distance from lat/lng

create a string with your zips inside the radius with php implode to get something like "100|101|104|105"

and then use a selector to select all dealer's pages that have one of those zip-codes in its pagefield: https://processwire.com/api/selectors/#or-selectors1

  • Like 1
Link to comment
Share on other sites

how many zip-codes do you have?

you could also create a matrix with all the distances between the zip-codes. you would only have to calculate the distances once and also the selection of the results would get far easier and more performant. https://processwire.com/talk/topic/8581-module-matrix-fieldtype-inputfield/

maybe a filter system like macrura did here fits your needs:

https://processwire.com/talk/topic/3671-ohmspeakercom/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...