Well, this is pain in a**... I not found the official docs.
My solution:
Swift 3.0
1) Do not use Storyboard, create mapView in code:
@IBOutlet weak fileprivate var mapViewContainer: UIView!
weak fileprivate var mapView: GMSMapView? {
didSet {
//you can config and customise your map here
self.configureMapView()
}
}
//If you use clustering
fileprivate var clusterManager: ClusterManager?
fileprivate var clusterRenderer: ClusterRenderer?
2) Create viewController configure function & call it before viewDidLoad:
func configure() {
mapView = GMSMapView()
mapView?.delegate = self
}
3) Layout & add mapView to your UI:
override func viewDidLoad() {
super.viewDidLoad()
if let mapView = mapView {
mapViewContainer.addSubview(mapView)
//Create constraints as you wish
mapView.fillView()
mapViewContainer.layoutSubviews()
}
}
4) Now create function:
func releaseMemory() {
if
let mapView = mapView,
let clusterManager = clusterManager {
clusterManager.clearItems()
mapView.clear()
mapView.delegate = nil
mapView.removeFromSuperview()
}
clusterManager = nil
clusterRenderer = nil
mapView = nil
}
You call it in ViewDidDisappear, or call delegate...
releaseMemory()
function clear memory, not so smart, but it works.