Liam Posted November 11, 2015 Share Posted November 11, 2015 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 More sharing options...
bernhard Posted November 12, 2015 Share Posted November 12, 2015 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... 1 Link to comment Share on other sites More sharing options...
Liam Posted November 12, 2015 Author Share Posted November 12, 2015 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 More sharing options...
bernhard Posted November 12, 2015 Share Posted November 12, 2015 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 1 Link to comment Share on other sites More sharing options...
bernhard Posted November 13, 2015 Share Posted November 13, 2015 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: http://ohmspeaker.com/speaker-filter/ (see advanced filter) https://processwire.com/talk/topic/3671-ohmspeakercom/ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now