gpt4 book ai didi

swift - 当我离开应用程序时,如何保存我的 MapView 注释?

转载 作者:行者123 更新时间:2023-11-30 10:35:34 25 4
gpt4 key购买 nike

func addPin(_ sender: UILongPressGestureRecognizer) {

let location = sender.location(in: self.mapView)
let locCoord = self.mapView.convert(location, toCoordinateFrom: self.mapView)

let annotation = MKPointAnnotation()

annotation.coordinate = locCoord
annotation.title = titleTextField.text

self.mapView.addAnnotation(annotation)

print("This will become the annotation title: \(titleTextField.text).")
print(annotation.coordinate.latitude, annotation.coordinate.longitude)

}

我是 swift 新手,想知道如何在离开应用程序后保存我的注释。另外,如果用户没有拿起手指,如何阻止形成多个注释?

最佳答案

您可能需要做的是将 MKPointAnnotation 存储为 UserDefaults 中的 Data。为此,您将根据所有 mapView 的注释创建一个字典数组:

func addPin(_ sender: UILongPressGestureRecognizer) {

let location = sender.location(in: self.mapView)
let locCoord = self.mapView.convert(location, toCoordinateFrom: self.mapView)

let annotation = MKPointAnnotation()

annotation.coordinate = locCoord
annotation.title = titleTextField.text

self.mapView.addAnnotation(annotation)

//Create a dictionary from the annotation
let newAnnotationDict = [
"lat": locCoord.latitude
"lng": locCoord.longitude
"title": annotation.title
]

//Pull the stored annotations data
var annotationsArray: [[String:Any]]!
var annotationsData = UserDefaults.standard.data(forKey: "StoredAnnotations")

//If the data is nil, then set the new annotation as the only element in the array
if annotationsData == nil {
annotationsArray = [newAnnotationDict]
} else {
//If it isn't nil, then convert the data into an array of dicts
do {
//Convert this data into an array of dicts
annotationsArray = try JSONSerialization.jsonObject(with: annotationsData, options: []) as! [[String:Any]]
annotationsArray.append(newAnnotationDict)
} catch {
print(error.localizedDescription)
}

}

do {

//Use JSONSerialization to convert the annotationsArray into Data
let jsonData = try JSONSerialization.data(withJSONObject: annotationsArray, options: .prettyPrinted)

//Store this data in UserDefaults
UserDefaults.standard.set(jsonData, forKey: "StoredAnnotations")
} catch {
print(error.localizedDescription)
}

print("This will become the annotation title: \(titleTextField.text).")
print(annotation.coordinate.latitude, annotation.coordinate.longitude)

}

然后,当再次打开 View Controller 时,您需要从 UserDefaults 中提取此 Data 并将其解析为 MKPointAnnotation< 的数组 对象。

class MapViewController: UIViewController {

//Whenever the view loads, check if there were any annotations stored last time
override func viewDidLoad() {
super.viewDidLoad()

//Check if the data for "StoredAnnotations" exists
if UserDefaults.standard.data(forKey: "StoredAnnotations") != nil {

var storedAnnotationObjects = [MKPointAnnotation]()

do {

//Get the data from UserDefaults
let storedAnnotationsData = UserDefaults.standard.data(forKey: "StoredAnnotations")!

//Convert this data into an array of dictionaries
let storedAnnotationsArray = try JSONSerialization.jsonObject(with: storedAnnotationsData, options: []) as! [[String:Any]]

for dict in storedAnnotationsArray {
//Initialize a new annotation and set the annotation's properties
let newAnnotation = MKPointAnnotation()
newAnnotation.coordinate = CLLocationCoordinate2D(latitude: dict["lat"] as! CGFloat, longitude: dict["lng"] as! CGFloat)
newAnnotation.title = dict["title"] as! String
newAnnotationObjects.append(newAnnotation)
}

//Add the annotation to the mapView
self.mapView.annotations = storedAnnotationObjects
} catch {
print(error.localizedDescription)
}
}
}

...

}

如果注释被删除,您需要将存储的数据类似地转换为字典数组,并删除适当的对象并更新数据。

关于swift - 当我离开应用程序时,如何保存我的 MapView 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58123822/

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