Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an image from Google Maps for iOS when user doesn't see map

I'm using Google Maps for iOS and I need to create a snapshot of a map. I know how to do that.

What I can't do is do it behind the scenes. I can't create a map that the user doesn't see, set up the bounds and markers, and then take a picture of it. The picture ends up blank.

The code (Swift) I am using is essentially

var mapView = GMSMapView(frame: CGRect(x: 0, y: 0, width: 300, height: 300));

I point the map then wait a while for the map tiles to load then

UIGraphicsBeginImageContext(mapView.frame.size);
mapView.layer.renderInContext(UIGraphicsGetCurrentContext());
var screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

But I just get a gray box with a Google logo at the bottom left.

like image 706
Robert Antonucci Avatar asked Nov 09 '22 18:11

Robert Antonucci


1 Answers

The following answer to a different question says that what I want is not possible with Google Maps for iOS.

https://stackoverflow.com/a/26279120/1672356

It suggests Google Maps' Static Maps web service as an alternative but I was using markers with custom icons and static maps requires you make those available publicly via some URL and I can't do that easily.

In the end I hacked it. I popup a big gray screen with a "Please Wait" message and behind it I have a Google Maps that I manipulate and take a screenshot of and then remove both map and gray screen and continue.

Ugh.

And I don't even know when the map is done loading, so I took a guess that 5 seconds will usually be enough time.

Sigh.

like image 105
Robert Antonucci Avatar answered Nov 14 '22 23:11

Robert Antonucci