开发者

Find the closest locations to a given address

开发者 https://www.devze.com 2022-12-28 19:26 出处:网络
I have built an application in CakePHP that lists businesses. There are about 2000 entries, and the latitude开发者_Python百科 and longitude coordinates for each business is in the DB.

I have built an application in CakePHP that lists businesses. There are about 2000 entries, and the latitude开发者_Python百科 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号