按距离搜索引擎
我希望在我的网站上选择我的搜索引擎,以便用户可以搜索设定距离内的项目,例如搜索 10 英里或 20 英里内的项目等。我想知道如何做到这一点?
用户必须输入他们的邮政编码,而我也有该项目位置的邮政编码,一旦他们点击搜索,就需要离开以英里为单位计算两个位置之间的距离,然后按距离顺序显示结果;因为最接近的项目是第一个结果。是否有谷歌 API 可以使用地图“获取方向”选项来计算距离(以英里为单位)?或者我可以添加到我的数据库中的东西?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Google Geocoding API 提供邮政编码查找,并且可以提供国家/地区、城市、纬度/经度(甚至仅提供邮政编码作为地址)。一旦获得了纬度/经度,您就可以轻松计算距离并对结果进行排序。
http://code.google.com/apis/maps/documentation/geocoding/ #GeocodingRequests
注意:您只能将 Geocoding API 与 Google 地图结合使用;禁止对结果进行地理编码而不将其显示在地图上。有关允许使用的完整详细信息,请参阅链接。
例如,如果您请求查找邮政编码 94043,则调用以下 URL:
这将产生 JSON,如下所示:
如果您由于某种原因无法使用 Google API,那么这里是非 Google Geocoder API 和服务的列表:
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:
Which would result with JSON such as following:
If you cannot use the Google API for some reason then here is list of non-Google Geocoder APIs and services: