问题
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