0

Some quick background, I perform a search location search request. The code for the search request is given below:

Search

-(void)performSearch {
    // activate search inidicator
    [self.completingSearchIndicator startAnimating];
    self.completingSearchIndicator.hidesWhenStopped = YES;
    self.doneButton.enabled = NO;
    self.cancelButton.enabled = NO;
    self.searchButton.enabled = NO;

    // Create a search request
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = self.searchText.text;

    // adjust the region of search so it is about 5000m x 5000m
    // about 2.5x bigger than viewing region
    // we should allow users to adjust this ****
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapHandle.userLocation.location.coordinate, 5000, 5000);;
    request.region = region;

    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
    [search startWithCompletionHandler:^ (MKLocalSearchResponse *response, NSError *error)
     {  // a block which loads each item into an array for us to use
        // an array of MKMapItem objects
         NSMutableArray *placemarks = [NSMutableArray array];
         for (MKMapItem *item in response.mapItems) {
             [placemarks addObject:item.placemark];
         }
         // save results in an instance variable
         self.searchResults = placemarks;

         // reactivate everything!
         [self.completingSearchIndicator stopAnimating];
         self.doneButton.enabled = YES;
         self.cancelButton.enabled = YES;
         self.searchButton.enabled = YES;
     }];
}

From that I receive an array of MKMapItems, I choose the ones I want to save, and store them in another array. From here I would like to print out their coordinates, however, I keep getting an error while trying to access them. The code I'm using to access them is:

    for (MKMapItem *item in self.selectedPlaces) {
        MKPlacemark *temp = item.placemark;
        CLLocationCoordinate2D coords = temp.coordinate;
        NSString *coordinateString = [NSString stringWithFormat:@"%f", coords.latitude];
        NSLog(@"%@",coordinateString);
    }

And the error I'm getting is:

2014-04-21 20:15:30.931 iTasks[2759:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKPlacemark placemark]: unrecognized selector sent to instance 0x193a5610'

Any resolution or alternate stategy would be appreciated! Many thanks in advance!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dylan
  • 697
  • 1
  • 9
  • 27
  • Try logging "item" and see if it's really an MKMapItem -- it seems from the error message that it's a MKPlacemark instead. – rdelmar Apr 22 '14 at 01:33
  • When `NSLog(@"@",item)` I am returned with `Old Chicago, 75 2nd St, Coralville, IA 52241-2603, United States @ <+41.66839200,-91.56319400> +/- 0.00m, region CLCircularRegion (identifier:'<+41.66835564,-91.56320944> radius 55.64', center:<+41.66835564,-91.56320944>, radius:55.64m)` – Dylan Apr 22 '14 at 01:36
  • looks like `item` is an MKPlacemark – lead_the_zeppelin Apr 22 '14 at 01:37
  • what is `selectedPlaces' ? – lead_the_zeppelin Apr 22 '14 at 01:38
  • selectedPlaces are the elements from my search request that I decided to save. I simply copied them from the search request output into a new list. I see messed up, in the above code I have a statement that saves only the placemarks: `for (MKMapItem *item in response.mapItems) { [placemarks addObject:item.placemark]; }` – Dylan Apr 22 '14 at 01:40

1 Answers1

0

Maybe you want this instead?

for (MKPlacemark *item in self.selectedPlaces) {
    CLLocationCoordinate2D coords = item.location.coordinate;
    NSString *coordinateString = [NSString stringWithFormat:@"%f", coords.latitude];
    NSLog(@"%@",coordinateString);
}
lead_the_zeppelin
  • 2,017
  • 13
  • 23