我试图了解CLPlacemark以及何时/如何为添加到地图的图钉标注创建信息。在几年前我在More 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属性,但这是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)我是否需要构建我的字符串,因为地址字典似乎已经对我有用了?
谢谢!
最佳答案
要回答您的第一个问题,请使用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];
}];
希望有帮助!