获取半径范围内的位置

时间:2013-07-09 13:02:09

标签: iphone ios ipad cllocationmanager cllocation

我正在开发iOS应用程序,我希望找到半径范围内的所有位置。

在objective-c中是否有任何方法可以指定固定的半径和位置,这将告诉我哪个位置在该半径范围内?

我做了一些研究,得到了这段代码,

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:locationManager.location
                   completionHandler:^(NSArray *placemarks, NSError *error) 
                   {
                       NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");

                       if (error)
                       {
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }


                       CLLocationDistance radius = 30;
                       CLLocation* target = [[CLLocation alloc] initWithLatitude:51.5028 longitude:0.0031];

                       NSArray *locationsWithinRadius = [placemarks objectsAtIndexes:
                                                         [placemarks indexesOfObjectsPassingTest:
                                                          ^BOOL(id obj, NSUInteger idx, BOOL *stop) {

                                                              return [(CLLocation*)obj distanceFromLocation:target] < radius;

                                                          }]];

                       NSLog(@"locationsWithinRadius=%@",locationsWithinRadius);


                   }];

但它会崩溃并显示错误:

由于未捕获的异常终止应用&#39; NSInvalidArgumentException&#39;,原因:&#39; - [CLPlacemark distanceFromLocation:]:

我是否正确行事?这是一种从我指定位置查找所有位置的方法吗?

提前致谢。

修改

NSArray *testLocations = @[[[CLLocation alloc] initWithLatitude:19.0759 longitude:72.8776]];

                   CLLocationDistance maxRadius = 3000; // in meters
                   CLLocation *targetLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]; //Current location coordinate..

                   NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(CLLocation *testLocation, NSDictionary *bindings) {
                       return ([testLocation distanceFromLocation:targetLocation] <= maxRadius);
                   }];

                   NSArray *closeLocations = [testLocations filteredArrayUsingPredicate:predicate];

                   NSLog(@"closeLocations=%@",closeLocations);

当我记录我的closeLocations数组时,它显示null(Empty)值。我在testLocations中提供的坐标靠近我当前的位置。

1 个答案:

答案 0 :(得分:4)

您在代码中尝试做的是地理编码,这是将坐标转换为地址的过程,而不是您想要做的事情。相反,您需要更基本的坐标边界。您可以在上面的代码中使用distanceFromLocation:方法,只需遍历坐标,将它们转换为CLLocation个对象(如果它们尚未存在),然后检查到中心点的距离。

我可能会使用indexesOfObjectsPassingTest和使用filteredArrayUsingPredicate创建的谓词来进行距离检查(除非您因某种原因确实需要索引),而不是使用predicateWithBlock。< / p>


NSArray *testLocations = @[ [[CLLocation alloc] initWithLatitude:11.2233 longitude:13.2244], ... ];

CLLocationDistance maxRadius = 30; // in meters
CLLocation *targetLocation = [[CLLocation alloc] initWithLatitude:51.5028 longitude:0.0031];

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(CLLocation *testLocation, NSDictionary *bindings) {
    return ([testLocation distanceFromLocation:targetLocation] <= maxRadius);
}];

NSArray *closeLocations = [testLocations filteredArrayUsingPredicate:predicate];