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

时间:2017-01-18 10:49:49

标签: ios mapkit

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

2 个答案:

答案 0 :(得分:9)

请使用以下代码:

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内,你可以获得国家/地区,城市等

答案 1 :(得分:5)

MKLocalSearchCompletion不包含地址"。它包含标题和副标题。要获取更多信息,请使用MKLocalSearchCompletion形成MKLocalSearchRequest并执行MKLocalSearch。