iphone谷歌地理编码请求失败

时间:2013-05-14 09:36:45

标签: iphone ios geocoding

我想使用谷歌地理编码来查找地名纬度经度名单的列表,我没有在iphone中使用它的经验,但在android中它只是调用网址。但是当我在iphone中尝试相同时,它会给我“拒绝请求”的错误。指导我如何将其集成到iPhone中或者我应该使用像BSForwardGeocoder这样的库,我这样做

NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", locationString];
url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSURL *wurl = [NSURL URLWithString:url];
NSDictionary *dict =  [Json objectWithUrl:wurl];
NSLog(@"%@", dict);

并打印为

{
results =     (
);
status = "REQUEST_DENIED";
}

3 个答案:

答案 0 :(得分:2)

检查此

NSString *locationString = @"1600 Amphitheatre Parkway, Mountain View, CA";
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", locationString];
url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSURL *wurl = [NSURL URLWithString:url];
NSData *data = [NSData dataWithContentsOfURL: wurl];

// Fail to get data from server
if (nil == data) {

    NSLog(@"Error: Fail to get data");
}
else{
    // Parse the json data
    NSError *error;
    NSDictionary *json = [NSJSONSerialization
                          JSONObjectWithData:data
                          options:kNilOptions
                          error:&error];

    // Check status of result
    NSString *resultStatus = [json valueForKey:@"status"];

    // If responce is valid
    if ( (nil == error) && [resultStatus isEqualToString:@"OK"] ) {

        NSLog(@"%@", json);
    }
}


参考:The Google Geocoding API

答案 1 :(得分:0)

您需要注册一个密钥,并且必须在请求中将其用作参数。

答案 2 :(得分:0)

尝试这个

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",address];