如何使用 subThoroughfare 属性初始化 MKPlacemark?

发布于 2024-12-09 12:26:21 字数 154 浏览 0 评论 0原文

在我的应用程序中,我正在创建自己的地标,并且需要将街道号码与街道名称分开。但我不知道如何使用地址字典初始化 MKPlacemark ,以便在 subThoroughfare 属性中返回街道号码,因为没有单独的 ABPerson 常量街道号码。

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 returned in the subThoroughfare property because there isn't a separate ABPerson constant for street number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宛菡 2024-12-16 12:26:21

我的解决方案是子类化 MKPlacemark 并覆盖地址属性,以识别一组自定义的地址字典键来匹配每个属性。

假设自定义地标类名为 CustomPlacemark,以下是为 addressDictionary 定义新键并重写属性方法的主要代码。

在 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;

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文