Objective-c search locations with radius

本小妞迷上赌 提交于 2019-11-30 20:17:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6195341/objective-c-search-locations-with-radius

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!