2015-05-26 33 views
2

我正在嘗試做簡單的事情。我想查找一組地址並在地圖上顯示它。但是,我需要將數據傳遞給另一個視圖。從數組中查找地址並將其發送給其他視圖

問題是:我需要傳遞包含地址的字典中的數據,所以我需要知道發現了哪個地址。請求只是異步和主線程,所以我不明白現在找到了什麼地址。

對不起,如果我說話不清楚。

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]; 
         } 
        } 
       }]; 

      } 

回答

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); 
    }]; 

} 
+0

感謝您的回答!但我也嘗試過這個變體......在這個變體中,我沒有找到正確的地址,也不明白爲什麼。舉個最好的例子:在NSLog中,它顯示沒有找到的地址。 –

+0

也許我誤解了一些東西。你的代碼的期望和實際行爲是什麼?請在回答中添加更多詳細信息 –

+0

Какяпонимаю,можнопо-русски? –

相關問題