iPhone 谷歌地理编码请求失败

标签 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";
}

最佳答案

检查这个

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

关于iPhone 谷歌地理编码请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16539740/

相关文章:

iphone - iOS查询多维数组并返回索引路径

ios - 如何在操作表而不是全屏模态视图中显示操作扩展?

ios - iOS 应用程序图标本地化不起作用

iPhone - 从照片中访问位置信息

google-maps - 使用谷歌地图进行区域偏差

php - 使用 MySQL 和 PHP 的 Google Geocode V2 与 V3

iphone - Xcode升级后报错: couldn't add 'com.apple.XcodeGenerated' tag

iphone - 为什么 Cocoa 偶尔会返回一个空字符串?

ios - 无法正确加载 iPhone 4"图像

ios - 如何以编程方式截取 iPad 主屏幕的屏幕截图?