我从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并不包含“地址”。它包含标题和子标题。如果需要更多信息,请使用MKLocalSearchCompletion创建MKLocalSearchRequest并执行MKLocalSearch。
item["place"]
,你就能得到实际的键。 - Poles