如何从 MKLocalSearchCompletion 中提取国家和城市?

How to extract country and city from MKLocalSearchCompletion?

我从 MKLocalSearchCompleter 接收到其委托的 MKLocalSearchCompletion 项目。每个 MKLocalSearchCompletion 包含一个标题和一个副标题,副标题是一个地址。我需要从地址中提取城市和国家。请帮忙!

请使用以下代码:

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"1 Infinite Loop Cupertino, CA 95014"; 
request.region = _mapView.region;
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    //NSLog(@"Map Items: %@", response.mapItems);
    if (response.mapItems.count>0) {
        for (MKMapItem *item in response.mapItems) {
            //NSLog(@"Place: %@",[item valueForKey:@"place"]);
            NSDictionary *dictStructuredAddress = [[[item valueForKey:@"place"] valueForKey:@"address"] valueForKey:@"structuredAddress"];
            NSLog(@"Structured Address : %@",dictStructuredAddress); 
            NSLog(@"Country & City : %@ & %@",[dictStructuredAddress valueForKey@"country"],[dictStructuredAddress valueForKey@"locality"]);
        }
}];

在这个dictStructuredAddress里面你可以得到国家,城市等等

MKLocalSearchCompletion 没有 "contain an address"。它包含标题和副标题。要获取更多信息,请使用 MKLocalSearchCompletion 形成 MKLocalSearchRequest 并执行 MKLocalSearch。