我正在尝试从我的iphone应用程序访问google地图的forward地理编码服务。当我试图从一个带有管道的字符串创建一个NSURL时,我得到的结果是一个空指针。
NSURL *searchURL = [NSURL URLWithString:@"http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false"];
我在google api中看不到任何其他方法来发送边界坐标而不使用管道。你知道我该怎么做吗?
发布于 2015-12-18 23:07:58
由于stringByAddingPercentEscapesUsingEncoding
已被弃用,因此您应该使用stringByAddingPercentEncodingWithAllowedCharacters
。
快速回答:
let rawUrlStr = "http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
let urlEncoded = rawUrlStr.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
let url = NSURL(string: urlEncoded)
编辑: Swift 3答案:
let rawUrlStr = "http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
if let urlEncoded = rawUrlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
let url = NSURL(string: urlEncoded)
}
发布于 2010-06-15 04:26:39
您是否尝试过用%7C
(字符|
的URL编码值)替换管道?
发布于 2014-01-22 23:53:11
如果你想在将来放入任何奇怪的字符都是安全的,使用stringByAddingPercentEscapesUsingEncoding
方法使字符串“URL友好”...
NSString *rawUrlStr = @"http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false";
NSString *urlStr = [rawUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *searchURL = [NSURL URLWithString:urlStr];
https://stackoverflow.com/questions/3040632
复制相似问题