1

I'm using CakePHP/mysql to build my app and I'm trying to get a list of cities within a given mile radius around an address both supplied by the user. So far I have a list of all US cities in a database with the long/lat. I also am using a CakePHP plugin to get the long/lat of all addresses inserted into the database.

Now how do I do the last part? I'm new to the google maps api but it looks like there is a limit on how many queries I make a day. The only way I can think to do it is to check the distance from the address and compare it to every city in the database and if it is within the given radius then they are selected. It seems like this would be way too database intensive and I would pass my query quotas in one go.

Any ideas?

Brian Jenkins
  • 349
  • 1
  • 6
  • 22
  • Do you want the math to determine the cities 1-mile from the given lat/lon, or do you want to display a google map of this data? – Ryre Oct 19 '11 at 18:18
  • I don't need a map, just a list of cities that are a certain distance from the lat/lon. Driving distance would be best, but a straight line would work as well. – Brian Jenkins Oct 19 '11 at 18:21
  • See http://stackoverflow.com/questions/7718785/mysql-calculating-distance-simple-solution – Ilmari Karonen Oct 19 '11 at 18:47
  • @IlmariKaronen That's exactly what I needed. Thanks! Now that I have the sql for it I need a new database of cities. Actually being able to look through it shows it's completely inconsistent with the lat/lon only to 1 or 2 decimals. – Brian Jenkins Oct 19 '11 at 21:20

1 Answers1

0

It sounds like you're trying to determine if a given point (city) is within a circle centered on a specific lat/lon. Here's a similar question.

Run a loop over each city and see if it satisfies the following condition:

if ( (x-center_x)2 + (y-center_y)2 <= radius2 )

If this is too slow, you could turn it into a lookup based on rounding the lat/lon. The more values you precompute, the closer to exact you can get.

Community
  • 1
  • 1
Ryre
  • 6,135
  • 5
  • 30
  • 47