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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With