1

Is there a library in for objective-c that will allow me to specify a radius and a location, and a list of locations and tell me which locations are within that radius? Thanks.

BukLau
  • 69
  • 3

1 Answers1

2

If you have CLLocations then something like this would work:

// 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];

There are other ways to do this of course. This is just one way.

Hope that helps.

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48