ios - 从数组中查找地址并将其发送到其他 View

标签 ios objective-c mapkit

我正在尝试做一些简单的事情。我想找到一组地址并将其显示在 map 上。但是我需要将数据传递给另一个 View 。

问题是:我需要从包含地址的字典中传递数据,所以,我需要知道找到了哪个地址。请求是异步的,并且只是主线程,所以我现在无法理解找到了什么地址。

对不起,如果我说的不清楚。

   for (NSDictionary *dic in adresses) {

            MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request];
            request.naturalLanguageQuery = [dic valueForKey:@"adress"];
            [search startWithCompletionHandler:^(MKLocalSearchResponse
                                                 *response, NSError *error) {


                if (response.mapItems.count == 0){

                    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
                    [dictionary setValue:request.naturalLanguageQuery forKey:@"realadress"];
                    [dictionary setValue:@"empty" forKey:@"itemname"];
                    [adressesonmap addObject:dictionary];
                    [array addObject:@"empty"];

//HERE I NEED TO KNOW WHICH ADRESS MAPKIT TRIED TO FIND!!!
                }
                else{
                    for (MKMapItem *item in response.mapItems)
                    {
                        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
                        [dictionary setValue:request.naturalLanguageQuery forKey:@"realadress"];
                        [dictionary setValue:item.name forKey:@"itemname"];
                        [adressesonmap addObject:dictionary];

                        [matchingItems addObject:item];
                        MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
                        annotation.coordinate = item.placemark.coordinate;
                        annotation.title = request.naturalLanguageQuery;
//HERE I NEED TO KNOW WHICH ADRESS MAPKIT TRIED TO FIND!!!

     // here I'me trying to get address, which I tried to find before in request, but it's always one of them (from request.naturalLanguageQuery)

                        [_mapView addAnnotation:annotation];
                        }
                    }
                }];

            }

最佳答案

据我了解,您需要知道异步请求找到的地址是什么。 您正在使用 block 来获取回调,因此您可以只使用 block 中外部范围的变量,它将捕获引用直到完成。

NSArray *adresses = @[
        @{@"adress":@"Kyiv, Gorkogo 17"},
        @{@"adress":@"New York"}
];

for (NSDictionary *dic in adresses) {
    MKLocalSearchRequest *request = [MKLocalSearchRequest new];
    request.naturalLanguageQuery = [dic valueForKey:@"adress"];
    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
    [search startWithCompletionHandler:^(MKLocalSearchResponse
                                         *response, NSError *error) {
        NSLog(@"\n\n\n+++++\nFound address: %@  response items:%@ ",dic[@"adress"], response.mapItems);
    }];

}

关于ios - 从数组中查找地址并将其发送到其他 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30457911/

相关文章:

ios - Objective-c 如何以编程方式导入文件?

ios - 如何使 UITextField 的文本在 iPhone 中以首字母大写显示

iPhone 应用程序 : MPMoviePlayer: PlayVideo in Landscape mode only

ios - 显示拖动的 MKAnnotationPin 的位置

ios - 为什么 Obj-C 实例有 1 个保留计数刚创建?

ios - UIBezierPath 绘制矩形以及所需的圆

iphone - 如何从服务器播放iPhone应用程序中的视频

ios - 在 iOS 的 UIWeb View 中禁用垂直滚动反弹?

iphone - 以真实分辨率将 MKMapView 渲染为 UIImage

ios - iOS 6 中的本地化地理编码选项