如何保存GMSCameraPosition?

时间:2016-09-19 16:30:28

标签: ios swift google-maps

在我的地图应用中,我想在应用终止之前保存相机位置。当应用程序再次启动时,我希望相机移动到保存的位置。这样,用户可以继续使用他/她上次离开的地图。

所以在包含地图视图的VC中,我添加了这个:

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))
}

这里的逻辑是当VC被取消初始化时,我将地图视图的摄像机位置保存到UserDefaults。然后在viewDidLoad中,我将相机移动到保存的位置。

当我运行应用程序时,我将相机移动到任意位置,按下Xcode中的停止按钮,然后再次打开应用程序。相机再次返回初始位置(0,0),而不是我将其移动到的任意位置。

调试后,我发现deinit甚至没有被调用!我真的很困惑。

这是保存相机位置的正确方法吗?我做错了什么?

1 个答案:

答案 0 :(得分:3)

  

将类声明为Final

final class viewController: UIViewController, GMSMapViewDelegate
  • 将mapView对象声明为Global:
  

ViewController.swift

var mapView:GMSMapView!
  • 制作单身类对象如下:
  

ViewController.swift

static let sharedInstance: MapController = MapController()
  • 从UserDefault获取存储,如下所示:
  

ViewDidLoad()中的ViewController.swift

let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 3)
    MapController.sharedInstance.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
 //    ...
    let tempUser: UserDefaults = UserDefaults.standard
    let longitude = tempUser.double(forKey: "lastLongitude")
    let latitude = tempUser.double(forKey: "lastLatitude")
    let zoom = tempUser.float(forKey: "lastZoom")
    let bearing = tempUser.double(forKey: "lastBearing")
    let viewingAngle = tempUser.double(forKey: "lastViewingAngle")
    MapController.sharedInstance.mapView.animate(to: GMSCameraPosition(target: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), zoom: zoom, bearing: bearing, viewingAngle: viewingAngle))
  

Userdefault 中保存数据时,在成功设置所有值后始终同步()数据。

  • 下面的函数每次mapView摄像头位置时保存数据。
  

ViewController.swift

func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
        let tempUser: UserDefaults = UserDefaults.standard
        tempUser.set(mapView.camera.target.longitude, forKey: "lastLongitude")
        tempUser.set(mapView.camera.target.latitude, forKey: "lastLatitude")
        tempUser.set(mapView.camera.zoom, forKey: "lastZoom")
        tempUser.set(mapView.camera.bearing, forKey: "lastBearing")
        tempUser.set(mapView.camera.viewingAngle, forKey: "lastViewingAngle")
        tempUser.synchronize()


}
  • 如果只想在应用终止时存储数据,请添加以下内容 代码到AppDelegate.swift:
  

Appdelegate.swift

func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        var saveLocation = MapController.sharedInstance
        let mapView = saveLocation.mapView as GMSMapView
        let tempUser: UserDefaults = UserDefaults.standard
        tempUser.set(mapView.camera.target.longitude, forKey: "lastLongitude")
        tempUser.set(mapView.camera.target.latitude, forKey: "lastLatitude")
        tempUser.set(mapView.camera.zoom, forKey: "lastZoom")
        tempUser.set(mapView.camera.bearing, forKey: "lastBearing")
        tempUser.set(mapView.camera.viewingAngle, forKey: "lastViewingAngle")
        tempUser.synchronize()

    }