查找距离给定地址最近的位置
我在 CakePHP 中构建了一个列出企业的应用程序。大约有2000个条目,每个商家的经纬度坐标都在数据库中。
我现在正在尝试解决搜索功能。
将有一个输入框,用户可以在其中输入街道地址、城市或邮政编码,然后我希望它返回从数据库中找到的 11 个最近的企业。
我该怎么做呢?
I have built an application in CakePHP that lists businesses. There are about 2000 entries, and the latitude and longitude coordinates for each business is in the DB.
I now am trying to tackle the search function.
There will be an input box where the user can put a street address, city, or zipcode, and then I would like it to return the 11 closest businesses as found from the database.
How would I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 Yahoo Geo Planet API 来识别与用户输入的搜索词相对应的位置。这通常会匹配多个位置,因此您必须将它们呈现给用户以让他们选择正确的位置。然后,一旦您知道了正确的位置,并且是 Yahoo API 提供的经纬度,您就可以使用 半正矢公式用于获取距离用户位置最近的企业。 这个问题的答案中有一个很好的例子。
I use the Yahoo Geo Planet API to identify the place corresponding to the search term the user entered. This normally matches multiple places, so you have to present them back to the user to get them to pick the right one. Then, once you know the right place, and it's lat longs, which the Yahoo API provides, you can use the haversine formula to get the closest businesses to the users location. There's a good example in the answer to this question.
我会通过在该点周围创建一个正方形来解决这个问题,要得到该点本身就是一个整体,因为您需要一个邮政编码数据库或 API,这往往会花钱。无论是购买数据库还是每次查找。
至少按城市或类似的方式进行操作,您可能(不确定)从 GMaps 返回该城市的长纬度。
然后我会尝试获取该点周围的 4 个角 longlat 坐标。然后我可以在数据库中搜索介于这些值之间的值。
不管怎样,我想说的是,这是一个很棘手的问题,但这是一个很好的问题!有兴趣看看大家的建议。
I'd approach this by creating a square around the point, to get the point is a whole thing in isself, as you'll need a postcode database, or api, which tend to cost money. Either in buying the database or per lookup.
Doing it by city or similar at least, you could probably, not sure, return the long-lat from GMaps for that city.
Then I'd try and get 4 corner longlat coords around that point. Then I could search the database for values which are between those.
Either way it's a tricky thing I'd say, fab question though! Interested to see peoples suggestions.