Swe*_*per 6 google-maps ios swift
在我的地图应用中,我想在应用终止之前保存相机位置.当应用程序再次启动时,我希望相机移动到保存的位置.这样,用户可以继续使用他/她上次离开的地图.
所以在包含地图视图的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))
}
Run Code Online (Sandbox Code Playgroud)
这里的逻辑是当VC被取消初始化时,我将地图视图的摄像机位置保存到UserDefaults.然后viewDidLoad,我将相机移动到保存位置.
当我运行应用程序时,我将相机移动到任意位置,按下Xcode中的停止按钮,然后再次打开应用程序.相机再次返回初始位置(0,0),而不是我移动到的任意位置.
调试后,我发现deinit甚至没有调用!我真的很困惑.
这是保存相机位置的正确方法吗?我做错了什么?
声明班级为 Final
final class viewController: UIViewController, GMSMapViewDelegate
Run Code Online (Sandbox Code Playgroud)
ViewController.swift
var mapView:GMSMapView!
Run Code Online (Sandbox Code Playgroud)
ViewController.swift
static let sharedInstance: MapController = MapController()
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
当在Userdefault中保存数据时,在所有值设置成功后始终同步()数据。
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()
}
Run Code Online (Sandbox Code Playgroud)
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()
}
Run Code Online (Sandbox Code Playgroud)