1

Very surprised this has been difficult to search. My hunt only showed me to use CLGeocoder for displaying the information in user friendly format.

I'm building a location based app, and I have functionality to save locations (to a server) and list them in a table view. But that is of little use when I cannot filter and arrange locations relevant to my area.

Should I do the heavy lifting of setting the location radius in the device, or in php?

My current idea for a solution is to just send a JSON string of coordinates to a php script that looks through a MySQL database for every entry that has a location field less than X away from the user's location (by a crude 'WHERE X<34.23049823 AND X>34.44789874 AND Y<...) type of thing. I don't think this is the easiest or quickest way to implement nearby location search functionality.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
user
  • 3,388
  • 7
  • 33
  • 67

1 Answers1

1

Of course you can do this in iOS.

// Given NSArray *locations as an array of CLLocation* that you wish to filter

// and given a radius...
CLLocationDistance radius = kSomeRadius;

// and given a target you want to test against...
CLLocation* target = [[CLLocation alloc] initWithLatitude:someLat longitude:someLon];


NSArray *locationsWithinRadius = [locations objectsAtIndexes:
                                 [locations indexesOfObjectsPassingTest:
                                  ^BOOL(id obj, NSUInteger idx, BOOL *stop) {

                                      return [(CLLocation*)obj distanceFromLocation:target] < radius;

                                  }]];

[target release];

source - Objective-c search locations with radius

Community
  • 1
  • 1
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • This is good, until I don't want to store every data point in a single array. Any idea on how I could best apply this to picking 30+- results from a (remote) database of 5000+? Would listing my database entries in order of lat/long be most time-wise? – user May 13 '13 at 01:31
  • listing by lat/long is one method or more natural to the user is the places sorted on increasing distance relative to his current location with the closest place listed first. Or any other heuristic like some places might be frequently visited by that users friend's (if you have that kind of info), hence bumping it to top. – Srikar Appalaraju May 13 '13 at 02:07
  • Well, I meant in the database. Of course I intend to show the closest locations in the app. But I am trying to figure how I should structure my database for searching locations, and the most efficient method for querying that database based on the user's location. – user May 13 '13 at 15:05
  • this would help - http://stackoverflow.com/questions/1370170/database-sql-how-to-store-longitude-latitude-data – Srikar Appalaraju May 13 '13 at 15:46
  • Nice find. Thanks, Srikar. – user May 13 '13 at 16:07