How to extract country and city from MKLocalSearchCompletion?

泪湿孤枕 提交于 2019-12-07 16:29:25

问题


I receive MKLocalSearchCompletion items from MKLocalSearchCompleter to its delegate. Every MKLocalSearchCompletion contains a title and a subtitle, the subtitle is an address. I need to extract a city and a country from the address. Please help!


回答1:


MKLocalSearchCompletion does not "contain an address". It contains a title and subtitle. To get more information, use the MKLocalSearchCompletion to form an MKLocalSearchRequest and perform the MKLocalSearch.




回答2:


Please use the following code:

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

Inside this dictStructuredAddress you can get country, city, etc.



来源:https://stackoverflow.com/questions/41717398/how-to-extract-country-and-city-from-mklocalsearchcompletion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!