Objective-c search locations with radius
Is there a library in for objective-c 开发者_如何学Gothat 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.
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.
精彩评论