1

I am looking to make an option of my serach engine on my site so that users can search for items within a set distance, e.g. search items within 10 miles, or 20 miles etc. I was wondering how this could be done?

The user would have to enter thier postcode, while i also have the postcode of the item's location and once they hit search there needs to be a away to work the distance between the two locations in miles and then display the results in order by distance; as in the closest item is the first result. Is there a google api for this as in use the maps 'get directions' option to work out the distance in miles? Or something i can add to my database?

Charles
  • 50,943
  • 13
  • 104
  • 142
Euan Hume
  • 119
  • 1
  • 4
  • 10
  • possible duplicate of [php/mysql zip code proximity search](http://stackoverflow.com/questions/2410529/php-mysql-zip-code-proximity-search) – John Conde Nov 18 '11 at 14:10
  • The thing you're looking for is a geospatial search. If you can tell us your language and database engines of choice, we can probably make worthwhile recommendations. – Charles Nov 19 '11 at 03:13
  • Thanks, im using PHP and MySQL – Euan Hume Nov 20 '11 at 19:43

1 Answers1

0

The Google Geocoding API provides zip code lookup and can provide the country, city, lat/lon given even just a zip code as the address. Once you have the lat/lon then you can easily calculate the distance and sort the results.

http://code.google.com/apis/maps/documentation/geocoding/#GeocodingRequests

Note: you can only use the Geocoding API in conjunction with a Google map; geocoding results without displaying them on a map is prohibited. For complete details on allowed usage, consult the link.

So if for example if you request lookup for zip code 94043 you call following URL:

http://maps.googleapis.com/maps/api/geocode/json?address=94043&sensor=false

Which would result with JSON such as following:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            },
           ...
          "location" : {
               "lat" : 37.4284340,
               "lng" : -122.07238160
          },
          "location_type" : "APPROXIMATE",
         ...
   "status" : "OK"
 }

If you cannot use the Google API for some reason then here is list of non-Google Geocoder APIs and services:

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75