1

ある半径内のすべての場所を見つけたい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);


                   }];

しかし、クラッシュしてエラーが表示されます:

キャッチされていない例外 'NSInvalidArgumentException' が原因でアプリを終了しています。理由: '-[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);

配列をログに記録すると、 closeLocationsnull(Empty) 値が表示されます。testLocations で指定した座標は、現在の場所に近いです。

4

1 に答える 1

4

コードでやろうとしているのはジオコーディングです。これは座標を住所に変換するプロセスであり、やりたいことではありません。代わりに、より基本的な座標境界が必要です。上記のコードでメソッドを使用しdistanceFromLocation:て、座標を反復処理し、それらをオブジェクトに変換してCLLocation(まだオブジェクトでない場合)、中心点までの距離を確認することができます。

を使用するのではなく、indexesOfObjectsPassingTestおそらくfilteredArrayUsingPredicateand で作成された述語を使用predicateWithBlockして、距離チェックを行います (何らかの理由で実際にインデックスが必要でない限り)。


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];
于 2013-07-09T13:18:00.650 に答える