# Calculating number of premises within 10 miles

Discussion in 'iOS Programming' started by snowjon, Jun 8, 2009.

1. ### snowjon macrumors newbie

Joined:
Jun 8, 2009
#1
Hi,

Please go easy - I'm a complete iPhone programming virgin!

I think what I am trying to do is fairly simple but any advice would be greatly appreciated. I am considering developing an iPhone app where users can submit the name and location of a particular type of business. A central server will store this info (coordinates and name), once it has been approved, and users will be able to search for these businesses within 10 miles of their current location.

Now, what would be the best way to conduct this search? I know we can get the users current position (coordinates) but what would be the best way to compare this to the coordinates in the central database and find how many business are located within 10 miles of their current location?

Many thanks!

jon

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#2
I think you are going to have to apply some Pythagorean theorem here. But you'll probably want to narrow down the list initially, before checking for within the 10-mile radius", by only looking at locations that fall within a 10-mile square using lats/longs. Hope that gets you started.

3. ### snowjon thread starter macrumors newbie

Joined:
Jun 8, 2009
#3
Yes that sounds like the kind of thing I need to do. However, how would you check the 10 mile distance based on lat/long? Is there a standard equation for that?

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#4
Actually, it's gonna be more like a 20-mile square, now that I think about it more. I don't know if there's a standard equation but when querying the server for the initial list you can exclude any locations whose latitude < current location - 10 miles or > current location + 10 miles. Similarly, do the same thing with longitude. (Not sure how longitudes lack of "parallel"-ness would affect that.) One degree of latitude is about 69 miles.

5. ### wlh99 macrumors 6502

Joined:
Feb 7, 2008
#5
Look at this website. There is a link to the formula that the site uses.

http://jan.ucc.nau.edu/~cvm/latlongdist.html

That said, you might be duplicating some of what Google maps will do on the iphone. Just to check, I hit the current location icon, and once it knew where I was, I typed hardware in the search bar. It found several hardware stores near me. I typed food, and it found a Safeway, and several restaurants.

Just pointing it out because you will need to differentiate your app from what is already on the phone to get it approved.

6. ### drivefast macrumors regular

Joined:
Mar 13, 2008
#6
most probably you will end up using CLLocation objects (essentially a class based on latitude-longitude tuples, with some bells and whistles attached); if this is the case, you will have a getDistanceFrom: function available, that you can use to calculate the distance between two points expressed as lat-long coordinates.

7. ### snowjon thread starter macrumors newbie

Joined:
Jun 8, 2009
#7
Excellent - thanks so much everyone! What a great community this is

8. ### sumantasau macrumors newbie

Joined:
Jun 24, 2009
#8
1° Latitude = 69.172 miles
1° Longitude = cos (Latitude) * 69.172 miles

If current location is 40.571429999999999, -122.35642

Latitude
((40.571429999999999*69.172)-10)/69.172 = 40.430968541750996
((40.571429999999999*69.172)+10)/69.172 = 40.715997165905278

longitude
(((cos(40.571429999999999)*69.172)*-122.35642)-10)/(cos(40.571429999999999)*69.172) = -122.54674119716235
(((cos(40.571429999999999)*69.172)*-122.35642)+10)/(cos(40.571429999999999)*69.172) = -122.16609880283772

SELECT * FROM TABLE_NAME WHERE (LAT BETWEEN 40.430968541750996 AND 40.715997165905278) AND (LONG BETWEEN -122.16609880283772 AND -122.54674119716235)

may this help