ios - 如何保存 GMSCameraPosition?

标签 ios swift google-maps

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

所以在包含 map View 的 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 被取消初始化时,我将 map View 的相机位置保存到 UserDefaults。然后在 viewDidLoad 中,我将相机移动到保存的位置。

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

调试后,发现连deinit都没有调用!我真的很困惑。

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

最佳答案

Declare class as Final

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

ViewController.swift

var mapView:GMSMapView!
  • 如下创建单例类对象:

ViewController.swift

static let sharedInstance: MapController = MapController()
  • 如下从 UserDefault 中获取:

ViewController.swift in ViewDidLoad()

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

When save data in Userdefault then always synchronize() data after all value are set successfully.

  • 每次在 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()

    }

关于ios - 如何保存 GMSCameraPosition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39577879/

相关文章:

ios - 如何检查字符串是否包含空格/字母数字/等字符?

ios - 如何在快速打开侧边菜单时添加透明背景?

google-maps - Google map api v3 : SearchBox. 模拟按下 Enter 按钮

android - 位置按钮不起作用 GoogleMaps v2

ios - 在 Swift 中解码 JSON Web token

ios - 有适用于 Xcode 11、iOS 13 的 iPhone SE 模拟器吗?

ios - 将按钮限制为 UIImageView 内的图像

具有关联类型的快速协议(protocol)

swift - 无法使用类型 'Int' 的参数列表调用类型 '(Number)' 的初始值设定项

javascript - Google Maps API - 自动居中和自动缩放