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

前端之家收集整理的这篇文章主要介绍了ios – 以编程方式/手动创建MKPlacemark / CLPlacemark前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题

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

讨论

看来,这个类只能被创建

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

他们的文件状态

You can create placemark objects manually for entities for which you already have address information,such as contacts in the Address Book. Creating a placemark object explicitly avoids the need to query the reverse geocoder object for the same information.

完善!我已经反向地理编码,并希望避免这样的查询.什么可以添加到字典?

For a list of strings that you can use for the keys of this dictionary,see the “Address Property” constants in ABPerson Reference. All of the keys in should be at the top level of the dictionary.

其中显示相关键

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

这相当于MKPlacemark的基本特征:

Accessing the Location Data

>位置属性

Accessing the Placemark Attributes

>名称属性
> addressDictionary属性
> ISOcountryCode属性
>国家财产
> postalCode属性
> administrativeArea属性
> subAdministrativeArea属性
>地方财产
> subLocality属性
>通道属性
> subThoroughfare属性
>区域属性

Accessing Geographic Information

>内陆水域财产
>海洋财产

Accessing Landmark Information

> areasOfInterest属性

幸运的是,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?谢谢.

解决方法

你可以继承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

它看起来很丑,但这是迄今为止我发现工作的唯一方法.

原文链接:https://www.f2er.com/iOS/335696.html

猜你在找的iOS相关文章