hpd*_*hpd 2 objective-c extend core-location ios clplacemark
虽然这里有一个类似的问题,但它没有提供答案,至少不是一般问题.
我的问题是:由于CoreLocation地理编码是速率有限的,我开发应用程序的(网络)服务提供了自己的后备地理编码服务,我想使用这个自定义地理编码服务,以防我达到Apple的速率限制.此外,我觉得避免使用此自定义REST API返回的结果的自定义数据类型是完全有意义的,因此希望使用返回的数据来生成CLPlacemarks.然而,该文档指出CLPlacemark如属性location, locality, administrativeArea等是read-only.因此,我创建了一个子类,CLPlacemark将所需的属性合成到我可以访问的私有变量上,即:
// interface: (.h)
@interface CustomPlacemark : CLPlacemark
- (nonnull id)initWithLocation: (nonnull CLLocation *)location
locality: (nullable NSString *)locality
administrativeArea: (nullable NSString *)adminArea
country: (nullable NSString *)country;
@end
// implementation (.m)
@implementation CustomPlacemark
@synthesize location = _location;
@synthesize locality = _locality;
@synthesize country = _country;
@synthesize administrativeArea = _administrativeArea;
- (nonnull id)initWithLocation: (nonnull CLLocation *)location
locality: (nullable NSString *)locality
administrativeArea: (nullable NSString *)adminArea
country: (nullable NSString *)country{
self = [super init];
if(self){
_location = location;
_locality = locality;
_administrativeArea = adminArea;
_country = country;
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
使用单元测试来测试此代码,该单元测试解析来自JSON文件initWithLocation: locality: administrativeArea: country:的数据并使用数据调用我的方法EXC BAD ACCESS (code=1)在测试结束时(在}测试方法结束时),地标变量指向nil尽管先前NSLog(@"placemark: %@", customPlacemark);输出正确的价值观 此外,逐行逐步测试显示CustomPlacemark工作(即指向正确填充的对象),直到测试结束.对我而言,这表明我解除了我CustomPlacemark的错误 - 但到底是什么?
任何帮助是极大的赞赏!
作为对任何登陆此类问题的人的参考:
经过一些密集的谷歌和深入潜入苹果的消息来源后,似乎CLPlacemark并不打算扩展.
但是,我能够基于此处提供的技巧实现变通方法,这基本上滥用了MKPlacemark扩展的事实,CLPlacemark并提供了使用自定义数据初始化的方法,即- (instancetype _Nonnull)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary<NSString *, id> * _Nullable)addressDictionary.找到用于addressDictionary映射所需属性的正确密钥CLPlacemark可能需要一些试验和错误,特别是因为ABPerson/AddressiOS 9已经弃用了功能.我为我的目的找到的密钥是:
@"City" -> CLPlacemark.city
@"State" -> CLPlacemark.administrativeArea
@"Country" -> CLPlacemark.country
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
495 次 |
| 最近记录: |