2015-02-24 41 views
0

这是我的代码:Objective-C的谷歌静态地图返回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返回nil - 想法?

回答

1

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

在创建url对象之前,请在您的stringURL上尝试呼叫stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

例如:

newStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];