title: How can I save a GMSCameraPosition?
body: <p>In my map app, I want to save the camera position just before the app terminates. When the app starts again, I want the camera to move to the saved position. This way, the user can continue using the map from where he/she left off last time.</p>
  
  <p>So in the VC that contains the map view, I added this:</p>
  
  <pre><code>deinit {
      let mapView = self.view as! GMSMapView
      UserDefaults.standard.set(mapView.camera.target.longitude, forKey: "lastLongitude")
      UserDefaults.standard.set(mapView.camera.target.latitude, forKey: "lastLatitude")
      UserDefaults.standard.set(mapView.camera.zoom, forKey: "lastZoom")
      UserDefaults.standard.set(mapView.camera.bearing, forKey: "lastBearing")
      UserDefaults.standard.set(mapView.camera.viewingAngle, forKey: "lastViewingAngle")
  }
  
  override func viewDidLoad() {
      let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 3)
      let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
      mapView.isMyLocationEnabled = true
      view = mapView
  
      mapView.delegate = self
  
      // ...
  
      let longitude = UserDefaults.standard.double(forKey: "lastLongitude")
      let latitude = UserDefaults.standard.double(forKey: "lastLatitude")
      let zoom = UserDefaults.standard.float(forKey: "lastZoom")
      let bearing = UserDefaults.standard.double(forKey: "lastBearing")
      let viewingAngle = UserDefaults.standard.double(forKey: "lastViewingAngle")
      mapView.animate(to: GMSCameraPosition(target: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), zoom: zoom, bearing: bearing, viewingAngle: viewingAngle))
  }
  </code></pre>
  
  <p>The logic here is that when the VC is deinitialized, I save the map view's camera position to <code>UserDefaults</code>. Then in <code>viewDidLoad</code>, I move the camera to the saved position.</p>
  
  <p>When I run the app, I moved the camera to an arbitrary position, pressed the stop button in Xcode, and opened up the app again. The camera is back at the initial position (0, 0) again, instead of the arbitrary position I moved it to.</p>
  
  <p>After debugging, I found that <code>deinit</code> isn't even called! I am really confused.</p>
  
  <p>Is this right way to save the camera position? What have I done wrongly?</p>