• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:23:36 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试做简单的事情。我想找到一组地址并将其显示在 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];
                        }
                    }
                }];

            }



Best Answer-推荐答案


据我了解,您需要知道异步请求找到了什么地址。 您正在使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap