我正在尝试了解 CLPlacemark 以及何时/如何为添加到地图的图钉标注创建信息。在几年前我在更多 iOS 3 开发中读到的内容之前,他们对地址进行了反向地理编码并构建了地址(街道、邮编、州等)。首先,我需要自己构建这个字符串吗?我试图找出如何获取某些已知事物的位置名称,例如在下面的代码中搜索苹果商店:
NSString *address = @"1 stockton, san francisco, ca";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
[placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"obj description: %@", [obj description]);
CLPlacemark *aPlacemark = (CLPlacemark *)obj;
NSLog(@"%@", [aPlacemark.addressDictionary description]);
NSLog(@"name: %@", ((CLPlacemark *)obj).name);
}];
];
当我打印出描述时,我看到控制台说:
Apple Store, San Francisco, 1 Stockton St, San Francisco, CA 94108-5805, United States @ <+37.78584545,-122.40651752> +/- 100.00m, region (identifier <+37.78584545,-122.40652161> radius 18.96) <+37.78584545,-122.40652161> radius 18.96m
旧金山 Apple Store 的名称从何而来?我认为这将是 CLPlacemark.name 属性,但那是空的。因此,在试图弄清楚 name 属性是如何创建的时,我发现:
NSLog(@"%@", [aPlacemark.addressDictionary description]);
我得到输出:
City = "San Francisco";
Country = "United States";
CountryCode = US;
FormattedAddressLines = (
"Apple Store, San Francisco",
"1 Stockton St",
"San Francisco, CA 94108-5805",
"United States"
);
PostCodeExtension = 5805;
State = California;
Street = "1 Stockton St";
SubAdministrativeArea = "San Francisco";
SubLocality = "Union Square";
SubThoroughfare = 1;
Thoroughfare = "Stockton St";
ZIP = 94108;
从这里,我只能看到,在 addressDictionary 的 FormattedAddressLines 键中,标题也在那里。
所以我想我的两个问题是:
1)如果有一个位置(即Apple Store),我如何获得位置的名称?
2)我是否需要再构建我的字符串,因为地址字典似乎已经为我做了这件事?
谢谢!