4

在对 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) from no error { "Cache-Control" = "public, max-age=86400"; “内容编码”= gzip;“内容长度”= 1603;“内容类型”=“应用程序/json;字符集=UTF-8”;日期 =“格林威治标准时间 2012 年 12 月 7 日星期五 08:51:58”;过期 =“星期六,2012 年 12 月 8 日 08:51:58 GMT”;服务器 = mafe;Vary = "接受语言"; “X-Frame-Options”=SAMEORIGIN;“X-XSS-保护”=“1;模式=块”;} 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,我会得到很多结果。

到目前为止,我已经尝试过:

  • 轻弹传感器布尔值真/假。
  • 将用户代理伪装成常规的 safari。
  • 使用 POST 而不是 GET。

没有运气...

4

1 回答 1

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]);
               }];
于 2012-12-09T02:01:24.660 回答