开发者

How to init an MKPlacemark with a subThoroughfare property?

In my app I'm creating my own placemarks and I need the street number separate from the street name. But I can't see how I can init an MKPlacemark with an address dictionary so that the street number is r开发者_StackOverflow中文版eturned in the subThoroughfare property because there isn't a separate ABPerson constant for street number.


My solution was to subclass MKPlacemark and override the address properties to recognise a custom set of address dictionary keys to match each of the properties.

Assuming the custom placemark class is named CustomPlacemark, here are the main bits of code to define the new keys for the addressDictionary and override the property methods.

in CustomPlacemark.h

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;

in CustomPlacemark.m

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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜