Search engine by distance

我只是一个虾纸丫 提交于 2019-12-11 04:35:29

问题


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?


回答1:


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:

  • http://code.google.com/p/gmaps-api-issues/wiki/NonGoogleGeocoders
  • http://webgis.usc.edu/Services/Geocode/About/GeocoderList.aspx


来源:https://stackoverflow.com/questions/8183628/search-engine-by-distance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!