【发布时间】:2015-03-09 11:11:29
【问题描述】:
我正在尝试弄清楚如何获取 MKPlacemark 商品的街道地址。我在控制台中打印了一个项目,我可以看到那里的信息,但我只得到 thoroughfare 信息,没有街道地址号码。
这是我的代码:
- (void)performSearch {
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc]init];
request.naturalLanguageQuery = _searchText.text;
request.region = _mapView.region;
_matchingItems = [[NSMutableArray alloc]init];
MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request];
NSLog(@"MKLocalSearch array created");
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if (response.mapItems.count == 0) {
NSLog(@"No Matches Found");
} else {
for (MKMapItem *item in response.mapItems) {
[_matchingItems addObject:item];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = item.placemark.coordinate;
// Pull out address info from MKMapItem
MKPlacemark *placemark = item.placemark;
NSLog(@"Placemark info: %@", item.placemark);
// Address details
NSDictionary *address = placemark.addressDictionary;
NSString *titleString = @"";
NSString *subtitleString = @"";
NSString *name = @"";
NSString *thoroughfare = @"";
NSString *state = @"";
NSString *city = @"";
NSString *country = @"";
name = [address objectForKey:@"Name"] ? [address objectForKey:@"Name"] : @"";
thoroughfare = [address objectForKey:@"Thoroughfare"] ? [address objectForKey:@"Thoroughfare"] : @"";
state = [address objectForKey:@"State"] ? [address objectForKey:@"State"] : @"";
city = [address objectForKey:@"City"] ? [address objectForKey:@"City"] : @"";
country = [address objectForKey:@"Country"] ? [address objectForKey:@"Country"] : @"";
titleString = [NSString stringWithFormat:@"%@ %@", name, thoroughfare];
subtitleString = [NSString stringWithFormat:@"%@ %@ %@ %@", thoroughfare, state, city, country];
// Strings for annotation
annotation.title = item.name;
annotation.subtitle = subtitleString;
[_mapView addAnnotation:annotation];
}
}
}];
}
【问题讨论】:
-
MKPlacemark 是 CLPlacemark 的子类。 CLPlacemark 为每个地址元素定义了方便的属性(因此您不必直接通过键名访问字典)。 Street# 应该在 placemark.subThoroughfare 中。但并非所有地址元素都可以保证设置。
-
谢谢!如果您将其设为答案,我会将其标记为您已回答。
标签: objective-c ios8 mapkit mkmapitem mkplacemark