BukLau
BukLau

Reputation: 69

Objective-c search locations with radius

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.

Upvotes: 1

Views: 1005

Answers (1)

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

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.

Upvotes: 2

Related Questions