views:

230

answers:

4

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?

A: 

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.

DavidYell
the us Census data can be used to generate population weighted long-lat vs Zip code databases. I've used that data in the past. If you google around, you will find a long-lat vs. zip code database out there for free. And then finding all zip codes that are within a square near the one in question is a pretty quick and easy to get a really good proximity look up.
Dan Berlyoung
A: 

this tutorial may helps you (turkish, but code examples is very understandable) http://phparchitect.org/tr/2010/04/lokasyon-bazli-servisler-yaratmak/

Osman Üngür
+2  A: 

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.

neilcrookes
This is really helpful and closer to what I'm looking for, thanks!
xtine
A: 

This might be handy,

http://phpadvent.org/2009/geoip-wrangling-by-andrei-zmievski

DavidYell