【问题标题】:Google Maps http request not working谷歌地图 http 请求不起作用
【发布时间】:2010-08-24 22:02:27
【问题描述】:

我正在尝试使用对谷歌地图的 http 请求来使用 JSON 获取两个位置之间的行驶距离。但是,这些在浏览器中的处理方式似乎与在 (iphone) 应用程序中的处理方式不同。

我使用坐标和 %20(空格)创建了一个保存 URL 的 NSString。我 NSLog() 确定 URL,它看起来没问题(即它在浏览器中工作正常并且 看起来 很好)......但是当 NSLogging 使用该 URL 的内容初始化字符串时,我得到(空)。

代码如下:

NSString *urlString=[[NSString alloc] initWithFormat:@"http://maps.google.com/maps/nav?q=from:%.7f%@%.7f%@to:%.7f%@%.7f", testLocation.coordinate.latitude, @"%%20", testLocation.coordinate.longitude, @"%%20", thePark.coordinate.latitude, @"%%20", thePark.coordinate.longitude];

然后我 NSLog() 并得到http://maps.google.com/maps/nav?q=from:51.4986110%20-0.1236110%20to:51.4960938%20-0.2200041 ...无济于事。

NSString *json=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];

当我 NSLog() 时,它会打印 null。有没有人对为什么会发生这种情况或更简单的方法有任何建议?我打算然后解析 JSON 并获得行驶距离

【问题讨论】:

    标签: iphone cocoa json google-maps


    【解决方案1】:

    您应该使用“stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding”转义 urlString,而不是使用 %20 手动转义。


    编辑:

    我用这3行代码测试,得到了json字符串。

    NSString *urlString=[[[[NSString alloc] initWithFormat:@"http://maps.google.com/maps/nav?q=from:%.7f %.7f to:%.7f %.7f", 51.4986110, -0.1236110, 51.4960938, -0.2200041] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] autorelease];
    
    NSString *json=[[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
    NSLog(@"json = %@", json);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多