0

我正在运行 MKLocalSearch 请求,当我找到 POI 时,我想根据我找到的 POI 的坐标放置一个图钉。NSLog(@"annotations %d",[mapView.annotations count]); 即使 POI 的坐标有效,也返回 0

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

}
4

0 回答 0