我正在努力做一件简单的事情。我想找到一个地址数组并在地图上显示。但后来我需要将数据传递给另一个视图。
问题是:我需要从包含地址的字典中传递数据,因此,我需要知道找到了哪个确切的地址。请求只是异步和主线程,因此我无法理解现在找到的地址。
对不起,如果我说不清楚。
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];
}
}
}];
}
答案 0 :(得分:1)
据我所知,你需要知道异步请求找到了什么地址。 您正在使用块来获取回调,因此您可以只使用块中外部作用域的变量,它将捕获引用直到完成。
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);
}];
}