objective-c google静态地图返回nil

时间:2015-02-24 15:34:05

标签: objective-c google-maps uiimage

这是我的代码:

#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会返回nil - ideas?

1 个答案:

答案 0 :(得分:0)

由于字符串中的某些字符,它可能返回了nil。

在创建stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding对象之前,请尝试在stringURL上调用url

例如:

newStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];