带有 RestKit 0.20 的 Google 地理编码 API
Posted
技术标签:
【中文标题】带有 RestKit 0.20 的 Google 地理编码 API【英文标题】:Google Geo Coding API with RestKit 0.20 【发布时间】:2013-01-24 19:58:05 【问题描述】:我想不出用一种优雅的方式解析这个响应。我正在尝试将不同的字段类型与包含以下属性的对象匹配。
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *address;
@property (nonatomic) NSString *city;
@property (nonatomic) NSString *postalcode;
@property (nonatomic) NSString *district;
@property (nonatomic) NSString *country;
过去,我浏览了字典,子字段与我搜索的字段相匹配。
"results" : [
"address_components" : [
"long_name" : "10",
"short_name" : "10",
"types" : [ "street_number" ]
,
"long_name" : "Warschauer Straße",
"short_name" : "Warschauer Straße",
"types" : [ "route" ]
,
"long_name" : "Friedrichshain",
"short_name" : "Friedrichshain",
"types" : [ "sublocality", "political" ]
,
"long_name" : "Friedrichshain-Kreuzberg",
"short_name" : "Friedrichshain-Kreuzberg",
"types" : [ "sublocality", "political" ]
,
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "locality", "political" ]
,
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "Germany",
"short_name" : "DE",
"types" : [ "country", "political" ]
,
"long_name" : "10243",
"short_name" : "10243",
"types" : [ "postal_code" ]
],
"formatted_address" : "Warschauer Straße 10, 10243 Berlin, Germany",
"geometry" :
"location" :
"lat" : 52.51329350,
"lng" : 13.45246930
,
"location_type" : "ROOFTOP",
"viewport" :
"northeast" :
"lat" : 52.51464248029150,
"lng" : 13.45381828029150
,
"southwest" :
"lat" : 52.51194451970850,
"lng" : 13.45112031970850
,
"types" : [ "street_address" ]
],
"status" : "OK"
【问题讨论】:
没有“优雅”的方式。只需对完整的 getter 进行硬编码,将它们映射到您在此处看到的 JSON。 【参考方案1】:好的。我想出了以下解决方案。我已经定义了一个 Dictionary 并重写了 Setter 以将传入的字典解析到我自己的解析器中:
-(void)addressByGoogle: (NSDictionary*)newAddressComponents
for (NSArray *googleResult in newAddressComponents)
for (NSArray *fields in googleResult)
if ([[fields valueForKey:@"types"] count] >= 1)
NSString *googleType = [[fields valueForKey:@"types"] objectAtIndex:0];
if ( [googleType isEqualToString:@"sublocality"])
self.district = [fields valueForKey:@"long_name"];
if ( [googleType isEqualToString:@"country"])
self.country = [fields valueForKey:@"long_name"];
if ( [googleType isEqualToString:@"locality"])
self.city = [fields valueForKey:@"long_name"];
if ( [googleType isEqualToString:@"street_number"])
self.streetnumber = [fields valueForKey:@"long_name"];
if ( [googleType isEqualToString:@"route"])
self.address = [fields valueForKey:@"long_name"];
if ( [googleType isEqualToString:@"postal_code"])
self.postalcode = [fields valueForKey:@"long_name"];
if ( [googleType isEqualToString:@"administrative_area_level_1"])
self.adminAreaLevel1 = [fields valueForKey:@"long_name"];
if ( [googleType isEqualToString:@"administrative_area_level_2"])
self.adminAreaLevel2 = [fields valueForKey:@"long_name"];
- (void) setAddressComponents:(NSDictionary *)newAddressComponents
[self addressByGoogle: newAddressComponents];
【讨论】:
【参考方案2】:正如Jim 所说,没有使用 Restkit 映射此对象的优雅方法,您可以做的是将这种格式直接映射到一个“丑陋的对象”并将该对象与另一个具有接口的对象包装为您想要。
类似:
@interface MYMappedAddress : NSObject
@property (nonatomic) NSArray *addressComponents;
@property (nonatomic) NSArray *types;
@property (nonatomic) MYGeometry *geometry;
@property (nonatomic) NSString * formattedAddress;
@end
@interface MYAddress : MYMappedAddress
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *address;
@property (nonatomic) NSString *city;
@property (nonatomic) NSString *postalcode;
@property (nonatomic) NSString *district;
@property (nonatomic) NSString *country;
@end
【讨论】:
以上是关于带有 RestKit 0.20 的 Google 地理编码 API的主要内容,如果未能解决你的问题,请参考以下文章
RestKit 0.20:restkit 对象映射使属性映射加倍