以编程方式/手动创建MKPlacemark / CLPlacemark

时间:2012-02-01 02:38:21

标签: ios cocoa-touch core-location

问题

我有一组地标信息(国家,城市等)和Lat / Lon对。我想用它来创建一个MKPlacemark对象。


讨论

看来这个类只能由

创建
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary

其文档陈述状态

  

您可以为已有地址信息的实体手动创建地标对象,例如地址簿中的联系人。明确地创建地标对象可以避免查询反向地理编码器对象以获取相同的信息。

完美!我已经进行了反向地理编码,并希望避免这样的查询。我可以在字典中添加什么内容?

  

有关可用于此字典键的字符串列表,请参阅ABPerson Reference中的“Address Property”常量。所有的键都应该在词典的顶层。

显示相关的密钥

const ABPropertyID kABPersonAddressProperty;
const CFStringRef kABPersonAddressStreetKey;
const CFStringRef kABPersonAddressCityKey;
const CFStringRef kABPersonAddressStateKey;
const CFStringRef kABPersonAddressZIPKey;
const CFStringRef kABPersonAddressCountryKey;
const CFStringRef kABPersonAddressCountryCodeKey;

这远远低于MKPlacemark的基本特征:

  

访问位置数据

  • 位置属性
  

访问地标属性

  • 名称属性
  • addressDictionary属性
  • ISOcountryCode属性
  • country property
  • postalCode属性
  • administrativeArea property
  • subAdministrativeArea property
  • locality property
  • subLocality属性
  • 通途财产
  • subThoroughfare property
  • region property
  

访问地理信息

  • 内陆水资源
  • 海洋财产
  

访问地标信息

  • areasOfInterest property

幸运的是,MKPlacemark超类的实际头文件说明了地址字典:

// address dictionary properties

@property (nonatomic, readonly) NSString *name; // eg. Apple Inc.

@property (nonatomic, readonly) NSString *thoroughfare; // street address, eg. 1 Infinite Loop

@property (nonatomic, readonly) NSString *subThoroughfare; // eg. 1

@property (nonatomic, readonly) NSString *locality; // city, eg. Cupertino

@property (nonatomic, readonly) NSString *subLocality; // neighborhood, common name, eg. Mission District

@property (nonatomic, readonly) NSString *administrativeArea; // state, eg. CA

@property (nonatomic, readonly) NSString *subAdministrativeArea; // county, eg. Santa Clara

@property (nonatomic, readonly) NSString *postalCode; // zip code, eg. 95014

@property (nonatomic, readonly) NSString *ISOcountryCode; // eg. US

@property (nonatomic, readonly) NSString *country; // eg. United States

@property (nonatomic, readonly) NSString *inlandWater; // eg. Lake Tahoe

@property (nonatomic, readonly) NSString *ocean; // eg. Pacific Ocean

@property (nonatomic, readonly) NSArray *areasOfInterest; // eg. Golden Gate Park

所以,我创建一个字典然后传递它:

return [[[MKPlacemark alloc] initWithCoordinate:aLocation.coordinate addressDictionary:addressDictionary] autorelease];

不幸的是,在这之后,内省显示信息没有坚持:

NSLog(@"placemark %@ from %@", placemark, addressDictionary);
NSLog(@"has %@", placemark.thoroughfare);

打印

2012-01-31 20:14:22.545 [15450:1403] placemark <+___,-___> +/- 0.00m from {
administrativeArea = __;
postalCode = _____;
subAdministrativeArea = ___;
subThoroughfare = __;
thoroughfare = "_____";
}
2012-01-31 20:14:22.545[15450:1403] has (null)

结论

所以,我到底在这里。有没有人想出如何创建自己的MKPlacemark?感谢。

1 个答案:

答案 0 :(得分:4)

您可以继承MKPlacemark

MyPlacemark.h

@interface MyPlacemark : MKPlacemark

extern NSString * const kCustomPlacemarkAddressThoroughfareKey;
extern NSString * const kCustomPlacemarkAddressSubThoroughfareKey;
extern NSString * const kCustomPlacemarkAddressLocalityKey;
extern NSString * const kCustomPlacemarkAddressSubLocalityKey;
extern NSString * const kCustomPlacemarkAddressAdministrativeAreaKey;
extern NSString * const kCustomPlacemarkAddressSubAdministrativeAreaKey;
extern NSString * const kCustomPlacemarkAddressPostalCodeKey;
extern NSString * const kCustomPlacemarkAddressCountryKey;
extern NSString * const kCustomPlacemarkAddressCountryCodeKey;

@end

MyPlacemark.m

#import "MyPlacemark.h"

@implementation MyPlacemark

NSString * const kCustomPlacemarkAddressThoroughfareKey = @"thoroughfare";
NSString * const kCustomPlacemarkAddressSubThoroughfareKey = @"subThoroughfare";
NSString * const kCustomPlacemarkAddressLocalityKey = @"locality";
NSString * const kCustomPlacemarkAddressSubLocalityKey = @"subLocality";
NSString * const kCustomPlacemarkAddressAdministrativeAreaKey = @"administrativeArea";
NSString * const kCustomPlacemarkAddressSubAdministrativeAreaKey = @"subAdministrativeArea";
NSString * const kCustomPlacemarkAddressPostalCodeKey = @"postalCode";
NSString * const kCustomPlacemarkAddressCountryKey = @"country";
NSString * const kCustomPlacemarkAddressCountryCodeKey = @"countryCode";

- (NSString *)thoroughfare
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressThoroughfareKey];
}

- (NSString *)subThoroughfare
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressSubThoroughfareKey];
}

- (NSString *)locality
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressLocalityKey];
}

- (NSString *)subLocality
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressSubLocalityKey];
}

- (NSString *)administrativeArea
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressAdministrativeAreaKey];
}

- (NSString *)subAdministrativeArea
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressSubAdministrativeAreaKey];
}

- (NSString *)postalCode
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressPostalCodeKey];
}

- (NSString *)country
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressCountryKey];    
}

- (NSString *)countryCode
{
    return [self.addressDictionary objectForKey:kCustomPlacemarkAddressCountryCodeKey];
}

@end

它看起来很难看,但这是迄今为止我发现工作的唯一途径。