objective-c google 静态地图 returns 无
objective-c google static map returns nil
这是我的代码:
#define GOOGLE_STATIC_MAP_IMAGE @"https://maps.googleapis.com/maps/api/staticmap?center=%@&zoom=15&size=320x150&maptype=roadmap&markers=color:green|label:H|%@"
-(UIImage*)GetGoogleStaticMap
{
NSString* coordinates = [NSString stringWithFormat:@"%f,%f", latitude, longitude];
NSString *stringURL = [NSString stringWithFormat:GOOGLE_STATIC_MAP_IMAGE, coordinates, coordinates]; //coordinates x2 = for the center & the marker
NSURL *url = [NSURL URLWithString:stringURL];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
如果我将 stringURL 粘贴到浏览器,我会得到想要的结果,但是 imageData returns nil - ideas?
可能由于字符串中的某些字符而返回 nil。
在创建 url
对象之前尝试在 stringURL
上调用 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
。
例如:
newStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
这是我的代码:
#define GOOGLE_STATIC_MAP_IMAGE @"https://maps.googleapis.com/maps/api/staticmap?center=%@&zoom=15&size=320x150&maptype=roadmap&markers=color:green|label:H|%@"
-(UIImage*)GetGoogleStaticMap
{
NSString* coordinates = [NSString stringWithFormat:@"%f,%f", latitude, longitude];
NSString *stringURL = [NSString stringWithFormat:GOOGLE_STATIC_MAP_IMAGE, coordinates, coordinates]; //coordinates x2 = for the center & the marker
NSURL *url = [NSURL URLWithString:stringURL];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
如果我将 stringURL 粘贴到浏览器,我会得到想要的结果,但是 imageData returns nil - ideas?
可能由于字符串中的某些字符而返回 nil。
在创建 url
对象之前尝试在 stringURL
上调用 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
。
例如:
newStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];