Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain a snapshot of Google Maps in iOS

In my iOS app, I intend to get a snapshot of my Google Maps map with the following code:

UIGraphicsBeginImageContext(self.mapView.frame.size)
if let context = UIGraphicsGetCurrentContext() {
    self.mapView.layer.render(in: context)
}
self.imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

However, the image returned is a black screen image with the Google Logo on the bottom left. What can I do to get the correct image snapshot of the map?

like image 821
mechdon Avatar asked Oct 25 '25 04:10

mechdon


1 Answers

Assuming mapView is an instance of GMSMapView, the following should work

let image = UIGraphicsImageRenderer(size: mapView.bounds.size).image { _ in
    mapView.drawHierarchy(in: mapView.bounds, afterScreenUpdates: true)
}

This is assuming the map has actually rendered before this code is called of course.

like image 50
hennes Avatar answered Oct 26 '25 17:10

hennes