Dayn Goodbrand
Dayn Goodbrand

Reputation: 611

Retrieve image from Google Maps in iOS

Im trying to attach a Google Maps image to an email generated in my app, however Google Maps does not return a specific image from its url. If I search a url with .png etc at the end of it, I can get the image fine, but how can I get the image from a site that doesn't have that,

ie:

http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap\\&markers=size:mid%%7Ccolor:red%%7CNew+York&sensor=false

Im using NSData dataWithContentsOfURL: which then attaches to my email.

I have also tried to open the url in an iframe within the email with the same result, works with a .png url, not maps.

Any help would be greatly appreciated, thanks.

Upvotes: 0

Views: 314

Answers (2)

Dayn Goodbrand
Dayn Goodbrand

Reputation: 611

http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap%%5C&markers=size:mid%%257Ccolor:red%%7CNew+York&sensor=false

Just needed to edit some of the % \ etc to incorporate the UTF8 encoding, however if you use the encoding method karthika suggested, it breaks the search. All I did was hard code the string and removed the added encoding before the address and its working perfectly.

Upvotes: 0

karthika
karthika

Reputation: 4091

Try this,

NSString *urlString = @"http://maps.googleapis.com/maps/api/staticmap?size=200x200&maptype=roadmap\\&markers=size:mid%%7Ccolor:red%%7CNew+York&sensor=false";
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSData *myData = [NSData dataWithContentsOfURL:webURL];
UIImage *image = [[UIImage alloc] initWithData:myData];
self.imageView.image = image;

Upvotes: 1

Related Questions