gpt4 book ai didi

ios - 如何保存 GMSCameraPosition?

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:59 25 4
gpt4 key购买 nike

在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com