Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the closest locations to a given address

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?

like image 244
xtine Avatar asked Nov 06 '22 13:11

xtine


1 Answers

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.

like image 120
neilcrookes Avatar answered Nov 12 '22 13:11

neilcrookes