Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top