我正在尝试了解CLPlacemark以及何时/如何为添加到地图的引脚的标注创建信息。在我几年前在更多iOS 3开发中阅读之前,他们反向对地址进行地理编码并构建地址(街道,邮政编码,州等)。首先,我需要自己构建这个字符串吗?我试图找出如何获取某些已知内容的位置名称,例如在下面的代码中搜索apple商店:
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属性,但它是null。因此,在尝试弄清楚如何创建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)我是否需要再构建我的字符串,因为地址字典似乎已经为我做了这个?
谢谢!
答案 0 :(得分:3)
您可以使用AddressBookUI框架中的ABCreateStringWithAddressDictionary函数从CLPlacemark的“addressDictionary”属性中获取地址字符串。
答案 1 :(得分:1)
要回答您的第一个问题,请使用CLPlacemark的areasOfInterest
属性。
至于第二个问题,那真的取决于你,以及你想如何格式化字符串。如果要使用addressDictionary
属性中的字符串构建它,那么一定要这样做。您可以选择和选择零件,并在完成处理程序中创建一个字符串。
另外,您提到要创建用于在地图上显示的注释。我个人将MKAnnotation子类化,并使用它快速创建一个注释以显示在地图上。
MyAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject <MKAnnotation> {
NSString *_name;
NSString *_address;
CLLocationCoordinate2D _coordinate;
}
@property (copy) NSString *name;
@property (copy) NSString *address;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
@end
MyAnnotation.m
#import "MyAnnotation.h"
@implementation MyAnnotation
@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate
{
if ((self = [super init])) {
_name = [name copy];
_address = [address copy];
_coordinate = coordinate;
}
return self;
}
- (NSString *)title
{
return _name;
}
- (NSString *)subtitle
{
return _address;
}
@end
声明一个NSMutableArray来保存所有注释,并将initWithName:address:coordinate:
抛出到你的完成处理程序中,你可以快速获得一个可以添加到地图中的注释数组。
NSString *address = @"1 stockton, san francisco, ca";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error)
{
NSMutableArray *annotationArray = [NSMutableArray array];
for (CLPlacemark *aPlacemark in placemarks)
{
NSString *nameString = // Get your name from an array created by aPlacemark.areasOfInterest;
NSString *addressString = // Put your address string info you get from the placemark here.
CLLocationCoordinate2D coordinate = aPlacemark.location.coordinate;
MyAnnotation *annotation = [[MyAnnotation alloc] initWithName:nameString address:addressString coordinate:coordinate];
[annotationArray addObject:annotation];
}
// Now save annotationArray if you want to use it later
// Add it to your MapView with [myMapView addAnnotations:annotationArray];
}];
希望有所帮助!