Welcome Guest, Not a member yet? Register   Sign In
Search by Zip Code and Distance

In CodeIgniter, what would be a good way to implement a search by Zip Code feature that also incorporated results a certain distance from Zip Code.

For example, a user might input their zip code and also select an option "Within 20 miles".

Then the results page would show results starting from closest to their zip code, and then list places further away ordered by distance up to 20 miles away.

There is an example on this page where it says: Search by Location. See  http://www.catholictherapists.com

On the example page, the location search also lets users search by city and state, and it has some auto-complete feature that starts suggesting places when you start typing a zip code ( and it says it is powered by Google).
Ideally I want to replicate some thing like this search by location, and the result page it produces.

Most people would do this via a map api like google maps.

Alternatively you could have a table of all postcodes and their lattitudes and longitudes and do some maths to calculate the max and min lattitudes and longitudes and select all postcodes within those ranges. This is quite complicated though, especially as here in the uk postcodes can be very strange shapes indeed. Simplified if you just use a square of side length = chosen distance, put the chosen postcode as the center and select all postcodes that those ranges include.

Either way, it would probably be better to use an api and rely on much bigger, better maintained map data than do it yourself. Then you could offer to search by city perhaps, or by driving time etc. Amazing what you can do with map api's these days.

This could maybe help you out.

Quote:This tutorial shows you how to build a Google Maps application for your website that allows your users to search for store locations that are nearest to them.

Google Maps Store locator

As others said, there are APIs that you can leverage to handle that. Some databases actually support geospatial searches either natively or through extensions/addons.

When it comes to geospatial searches, I create a collection in Apache Solr and go that route. It is better for searches anyway.

Thanks to everyone for the feedback.

It does seem using the Google Map API makes sense. I'll have to work through the tutorials on the Google Map API pages to fully understand how  I might integrate it into a Codeigniter built website.

I see I will also need an additional feature for my website's administration area where I would enter the store addresses.

In that case, the address or zip code would need to be converted to latitude and longitude values and stored in the database that way. It seems the Google Map API can also help with that.

I found this PDF very helpful when doing something similar on a website I work on:


If somebody is giving you their zip, then why not their address? Once you have the address, getting the geo coordinates from Google is simple, then use the coordinates in a mysql query to get all locations within your radius. Order by distance of course.

Theme © iAndrew 2016 - Forum software by © MyBB