开发者

Objective-c search locations with radius

开发者 https://www.devze.com 2023-03-09 09:38 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消