views:

123

answers:

1

I'm trying to show a small static google map in my app. I'm wondering if it's possible to do this using UIImageView? It doesn't seem to be working for me but maybe I'm doing something wrong.

This is what I have (I stripped the URL so that it's easier to read):

NSURL *mapurl = [[NSURL alloc]    initWithString:@"http://maps.google.com/maps/api/staticmap?[...]"]; 
NSData *mapdata = [[NSData alloc] initWithContentsOfURL:mapurl];
UIImage *uimap = [[UIImage alloc] initWithData:mapdata];
self.map.image = uimap; // map is my UIImageView    
[mapurl release];   
[mapdata release];
[uimap release];

I know my UIImageView is setup correctly because if I replace the URL with one of an image then it loads fine. Also I know the google maps URL is correct because if I load it straight into a browser it shows up fine.

I'm starting to think that this might not be possible. If anyone has any ideas or alternatives please share.

Thanks.

A: 

Well I ended up finding a solution for this so I'll post it in case someone else runs into the same issue.

First I changed the way I load the image to using NSURLConnection. I don't think this particular change makes a difference but this is what allowed me to find the problem. When trying to load the URL I got a "bad url" error. So I did some searching on that and turns out there are some invalid characters in the google static map URL that must be escaped.

So basically just call this on the url and you should be good to go:

// The URL you want to load (snipped for readability)
NSString *mapurl = @"http://maps.google.com/maps/api/staticmap?[...]";  

// Escape the string
mapurl = [mapurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

I haven't tested this solution with the code I have in my original post but I did test it with my new NSURLConnection implementation and it works perfectly. I assume it'll work fine with the code above as well since it was just a matter of escaping the string.

Nebs