Holen Sie sich Standorte in einem Umkreis

Ich entwickle eine iOS-App, in der ich alle Orte in einem gewissen Umkreis finden möchte.

Gibt es in Objective-C eine Möglichkeit, einen festen Radius und einen Ort anzugeben, und die Aufschluss darüber gibt, welche Orte sich innerhalb dieses Radius befinden?

Ich habe einige Nachforschungen angestellt und habe diesen Code-Schnipsel erhalten.

- (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);


                   }];

aber es kommt zu Abstürzen und zeigt Fehler:

App wird aufgrund einer nicht erfassten Ausnahme 'NSInvalidArgumentException' beendet, Grund: '- [CLPlacemark distanceFromLocation:]:

Gehe ich auf dem richtigen Weg? Ist dies eine Möglichkeit, um alle Standorte von meinen angegebenen Standorten zu finden?

Danke im Voraus.

BEARBEITEN:

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);

Wenn ich mich anmeldecloseLocations Array zeigt den Wert null (leer). Die in testLocations angegebene Koordinate befindet sich in der Nähe meines aktuellen Standorts.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage