在对CLGeocoder非常失望之后,我决定改用GoogleMaps API。
我使用AFNetwork设计了如下呼叫:
AFHTTPClient *new = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"thorsgade",@"true", nil] forKeys:[NSArray arrayWithObjects:@"address",@"sensor", nil]];
NSMutableURLRequest *req = [new requestWithMethod:@"GET" path:@"maps/api/geocode/json" parameters:dict];
AFJSONRequestOperation *call = [AFJSONRequestOperation JSONRequestOperationWithRequest:req success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSArray *geos = [JSON objectForKey:@"results"];
DLog(@"Got result : '%@' %@ from %@ %@ %@",JSON,geos,[NSHTTPURLResponse localizedStringForStatusCode:response.statusCode],response.allHeaderFields,request.URL.description);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
DLog(@"Failed %@ %@",error.localizedDescription,request.URL.description);
}];
[call start];
我得到了这样的反馈:
得到结果:'(null)'(null)来自无错误{ “Cache-Control”=“public,max-age = 86400”; “Content-Encoding”= gzip; “内容长度”= 1603; “Content-Type”=“application / json; charset = UTF-8”; 日期=“星期五,2012年12月7日08:51:58 GMT”; Expires =“星期六,2012年12月8日08:51:58 GMT”; Server = mafe; Vary =“接受语言”; “X-Frame-Options”= SAMEORIGIN; “X-XSS-Protection”=“1; mode = block”; } http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=thorsgade
空结果,但没有错误。内容在标头中被识别为JSON,但原始JSON为空。
令人讨厌的是,如果我在浏览器中打开http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=thorsgade,我会得到很多结果。
到目前为止,我已尝试过:
没有运气......
答案 0 :(得分:1)
如果问题仍然存在,我建议改为使用MKNetworkKit
这是我的解决方案 -
<强> GoogleGeocodeApi.h 强>
//GoogleGeocodeApi.h
#import <Foundation/Foundation.h>
#import "MKNetworkEngine.h"
typedef void (^JsonResponseBlock)(NSDictionary *);
typedef void (^ErrorBlock)(NSError* error);
@interface GoogleGeocodeApi : MKNetworkEngine
-(MKNetworkOperation*) geocodeWithAddress: (NSString *) address
onCompletion:(JsonResponseBlock) completionBlock
onError:(ErrorBlock) errorBlock;
@end
<强> GoogleGeocodeApi.m 强>
//GoogleGeocodeApi.m
#import "GoogleGeocodeApi.h"
@implementation GoogleGeocodeApi
-(id)init
{
if (self = [super initWithHostName:@"maps.googleapis.com" apiPath:@"maps/api/geocode" customHeaderFields:nil]) {
}
return self;
}
-(MKNetworkOperation*) geocodeWithAddress: (NSString *) address
onCompletion:(JsonResponseBlock) completionBlock
onError:(ErrorBlock) errorBlock;
{
MKNetworkOperation *op = [self operationWithPath:[NSString stringWithFormat:@"json?sensor=true&address=%@", address] params:nil httpMethod:@"GET"];
[op onCompletion:^(MKNetworkOperation *completedOperation) {
NSDictionary *responseJSON = [completedOperation responseJSON];
if (responseJSON && [[responseJSON objectForKey:@"status"] isEqualToString:@"OK"]) {
completionBlock(responseJSON);
} else {
NSDictionary* errorDictionary = @{NSLocalizedDescriptionKey :@"Google geocode failed!"};
NSError *error = [NSError errorWithDomain:@"Failed response" code:100 userInfo:errorDictionary];
errorBlock(error);
}
} onError:^(NSError* error) {
errorBlock(error);
}];
[self enqueueOperation:op];
return op;
}
代码中的某处
GoogleGeocodeApi *gma = [[GoogleGeocodeApi alloc] init];
[gma geocodeWithAddress:@"thorsgade"
onCompletion:^(NSDictionary *responseJSON) {
NSLog(@"Geocode succeeded: %@", responseJSON);
} onError:^(NSError *error) {
NSLog(@"Geocode failed with error: %@", [error localizedDescription]);
}];