0

I'm running a MKLocalSearch request and when I find POIs I want to drop a pin based on the coordinates of the POIs I find. NSLog(@"annotations %d",[mapView.annotations count]); returns 0 even with valid coordinates for the POIs

- (void)resultsForButtonTapped:(NSString *)search
{

arrayOfPOIs = [[NSMutableArray alloc] init];
arrayOfAnnotaions = [[NSMutableArray alloc] init];
NSLog(@"%@",search);
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
MKCoordinateRegion region;
request.naturalLanguageQuery = search;
request.region = region;

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localsearch = [[MKLocalSearch alloc] initWithRequest:request];

[localsearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if (error != nil) { 

        return;
    }else{
        if ([response.mapItems count] == 0) {
            [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
                                    message:nil
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
            return;
        }
        for (MKMapItem *item in response.mapItems)
        {
            NSString *name = item.name;
            NSLog(@"%@",name);
            [arrayOfPOIs addObject:name];
        }

        results = response;
        NSLog(@"%d",[arrayOfPOIs count]);

        for (int i=0; i<[results.mapItems count]; i++) {
            MKMapItem* itemPOI = results.mapItems[i];
            NSLog(@"Result: %@",itemPOI.placemark.name);


            NSLog(@"Result: %@",itemPOI.placemark.name);

            MKPlacemark* annotation= [[MKPlacemark alloc] initWithPlacemark:itemPOI.placemark];

            MKPointAnnotation *marker = [MKPointAnnotation new];
            marker.coordinate = annotation.coordinate;

            [mapView addAnnotation:marker];
            NSLog(@"annotations %d",[mapView.annotations count]);
        }

        [self loadTheDataAgain];
    }
}];
  //    NSLog(@"outside the block, the count is %lu",(unsigned long)[arrayOfPOIs count]);

    NSLog(@"stuff %@",arrayOfAnnotaions);

}
Marco
  • 6,692
  • 2
  • 27
  • 38
  • In that same NSLog, make sure mapView is not nil. Eg. `NSLog(@"annotations %d, mapView=%@",[mapView.annotations count], mapView);` –  Jan 21 '14 at 02:02
  • After playing around with it, mapView is null inside this specific function. I'm instantiating mapView in my viewDidLoad method and subclassing it inside a uitableview cell. Inside the cell for row at index path method, mapView is not null – Chris McGrath Jan 21 '14 at 20:44
  • Is the resultsForButtonTapped method in the same view controller as the table view? From where and when is resultsForButtonTapped called? Posting the cellForRowAtIndexPath method may help. –  Jan 21 '14 at 21:04
  • Yes, it's in the same view controller. mapView is a property of my view controller btw – Chris McGrath Jan 21 '14 at 21:14

0 Answers0